Numerical comparison

Post Reply
abhishek.thakur
Posts: 5
Joined: Thu Aug 26, 2010 11:29 am

Numerical comparison

Post by abhishek.thakur »

I was comparing 2 numerical values in my vortex script and came around with this strange issue that there is a difference in comparison when I compare a 3 digit number with a 3 digit number and when I compare the same 3 digit number with a four digit number.

For example:
Scenario 1 : (comparing 3 digit number with 4 digit number)

The example code is as follows :

<strfmt "%d" $age><$age=$ret>Age:$age
<strfmt "%d" $no_of_downloads><$no_of_downloads=$ret>dwnd:$no_of_downloads
<if $no_of_downloads lt 1000>
Hello 1
</if>
<if $age le 1000 >
Hello 2
</if>

Output :
Age:761
dwnd:526

Scenario 2: (comparing 3 digit number with 3 digit number)

The example code is as follows :
<strfmt "%d" $age><$age=$ret>Age:$age
<strfmt "%d" $no_of_downloads><$no_of_downloads=$ret>dwnd:$no_of_downloads
<if $no_of_downloads lt 999>
Hello 1
</if>
<if $age le 999 >
Hello 2
</if>

Output :
Age:761
dwnd:526
Hello 1
Hello 2

Scenario 3:

The example code is as follows :
<strfmt "%d" $age><$age=$ret>Age:$age
<strfmt "%d" $no_of_downloads><$no_of_downloads=$ret>dwnd:$no_of_downloads
<if $no_of_downloads lt 1000>
Hello 1
</if>
<if $age le 999 >
Hello 2
</if>

Output :
Age:761
dwnd:526
Hello 2

This same kind of thing happened when I tried with 2 digit and 3 digit numbers.
Could anyone please suggest a better way to handle such scenarios.
User avatar
mark
Site Admin
Posts: 5519
Joined: Tue Apr 25, 2000 6:56 pm

Numerical comparison

Post by mark »

<strfmt> produces strings.
<strfmt "%d" $age> treats it argument as a number and formats it into a string accordingly.
So you're doing string comparisons.

To force vars into a type use convert():
<$age=(convert($age, 'long' ))>
<$no_of_downloads=(convert($no_of_downloads, 'long' ))>
Post Reply