Vishal Patil
Vishal Patil

Reputation: 305

How to compare two variables using If Else in NSIS?

Var first
Var second
Section
   Strcpy $first "1.0"
   Strcpy $Second "2.1"
   ${If} $second > $first
     MessageBox MB_OK "Grater"
   ${Else}
     MessageBox MB_OK "Smaller"
   ${EndIf}
SectionEnd

I have written the above code but it is showing me result as smaller. And how to compare a integer or double value coming from a text file with a predefined double or integer value?

Upvotes: 5

Views: 5817

Answers (2)

Peyman Mohamadpour
Peyman Mohamadpour

Reputation: 17964

Using LogicLib, You may compare two integers like this:

Var first
Var second
Section
   StrCpy $first 1
   StrCpy $Second 2
   ${If} $second > $first
     MessageBox MB_OK "Grater"
   ${Else}
     MessageBox MB_OK "Smaller"
   ${EndIf}
SectionEnd

with capital C in StrCpy. Also try removing quotes (") from numbers to make them integers.

Another way would be this:

Push $first
Push $Second
StrCpy $first 8
StrCpy $Second 2

IntCmp $first $Second Equal Val1Less Val1More

Equal:
    DetailPrint "$first = $Second"
    Goto End
Val1Less:
    DetailPrint "$first < $Second"
    Goto End
Val1More:
    DetailPrint "$first > $Second"
    Goto End
End:

Pop $Second
Pop $first

Upvotes: 5

Anders
Anders

Reputation: 101736

NSIS does not support floating point numbers in the basic instructions, you need to use the Math plugin that is part of the default install...

Upvotes: 1

Related Questions