Page 1 of 1

Problem with decimal separator.

Posted: 14 Oct 2011 18:06
by tiago
//right
$v1 = "1424000"; $v2 = "418000"; IF ($v1 > $v2) { echo bigger; } ELSEIF ($v1 <= $v2) { echo smaller; };

//wrong. Why?
$v1 = "1.424.000"; $v2 = "418.000"; IF ($v1 > $v2) { echo bigger; } ELSEIF ($v1 <= $v2) { echo smaller; };

Re: Problem with decimal separator.

Posted: 15 Oct 2011 00:19
by nas8e9
Best guess: the first example - minus the dots - is interpreted as numbers, while the numbers with dots in them, are interpreted as strings. The string comparison probably looks at the first character alphabetically, where 4 is larger than 1.

Re: Problem with decimal separator.

Posted: 17 Oct 2011 12:59
by TheQwerty
Also keep in mind this from the Scripting > Math section of help:
(2) The decimal separator is NOT locale specific (e.g. dot in US, comma in Germany) but hard-coded to dot (period). This way scripts are interchangeable between regions.

Re: Problem with decimal separator.

Posted: 17 Oct 2011 13:12
by nas8e9
TheQwerty wrote:Also keep in mind this from the Scripting > Math section of help:
(2) The decimal separator is NOT locale specific (e.g. dot in US, comma in Germany) but hard-coded to dot (period). This way scripts are interchangeable between regions.
Useful (if sometimes forgotten :oops:) resource, the help file.

Going by this section:
Strings and Numbers

If you compare two numerical strings, they are compared as integers:

msg ("2" < "10"); // -> true! (both parts are numeric)

msg ("2" < "10a"); // -> false! (only one parts numeric)

msg ("2a" < "10a"); // -> false! (both parts are non-numeric)
...I was right about assuming a string comparison and wrong about XYplorer not supporting decimal points (albeit not localised and with thousands separators not supported).

Re: Problem with decimal separator.

Posted: 18 Oct 2011 03:36
by tiago
Being everything but obvious I think this should be mentioned on the Math section with proper highlights.
Example

Code: Select all

text format(123456, "#,0"); //123.456
should be added, I think.
But the best way should be really doing the usual 1.424.000 > 418.000.

Re: Problem with decimal separator.

Posted: 18 Oct 2011 09:24
by admin
tiago wrote:Being everything but obvious I think this should be mentioned on the Math section with proper highlights.
Example

Code: Select all

text format(123456, "#,0"); //123.456
should be added, I think.
But the best way should be really doing the usual 1.424.000 > 418.000.
There is no variable typing in XY scripting. So the script engine must guess whether you want a numerical or a string comparison.

BTW, I don't know of any programming language where you can state literal numbers with thousand separators AND treat them as numbers. Thousand separators are a display format for numbers, they are not "really" part of the numbers.