Problem with decimal separator.

Things you’d like to miss in the future...
Forum rules
You might want to read this if you're looking for priority service

:info: Please Include The Following Information
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).

:info: Use Your Profile Or Signature
We strongly recommend adding your Windows Version and Screen Scaling Percentage to the Location field in your Profile or to your Signature. That way, you only have to type them once, and we won't have to search for that vital information.

:info: How To Attach Images
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
Post Reply
tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Problem with decimal separator.

Post 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; };
Power-hungry user!!!

nas8e9
Posts: 2232
Joined: 21 Jun 2008 14:50

Re: Problem with decimal separator.

Post 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.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Problem with decimal separator.

Post 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.

nas8e9
Posts: 2232
Joined: 21 Jun 2008 14:50

Re: Problem with decimal separator.

Post 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).

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: Problem with decimal separator.

Post 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.
Power-hungry user!!!

admin
Site Admin
Posts: 67332
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Problem with decimal separator.

Post 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.

Post Reply