//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; };
Problem with decimal separator.
Forum rules
You might want to read this if you're looking for priority service
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%).
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.
How To Attach Images
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
You might want to read this if you're looking for priority service
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%).
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.
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
-
nas8e9
- Posts: 2232
- Joined: 21 Jun 2008 14:50
Re: Problem with decimal separator.
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.
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.
Useful (if sometimes forgottenTheQwerty 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.
Going by this section:
...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).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)
-
tiago
- Posts: 589
- Joined: 14 Feb 2011 21:41
Re: Problem with decimal separator.
Being everything but obvious I think this should be mentioned on the Math section with proper highlights.
Example
should be added, I think.
But the best way should be really doing the usual 1.424.000 > 418.000.
Example
Code: Select all
text format(123456, "#,0"); //123.456But 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.
There is no variable typing in XY scripting. So the script engine must guess whether you want a numerical or a string comparison.tiago wrote:Being everything but obvious I think this should be mentioned on the Math section with proper highlights.
Exampleshould be added, I think.Code: Select all
text format(123456, "#,0"); //123.456
But the best way should be really doing the usual 1.424.000 > 418.000.
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.
FAQ | XY News RSS | XY X
XYplorer Beta Club