SC Format: number fraction handling?

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".
Filehero
Posts: 2734
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

SC Format: number fraction handling?

Post by Filehero »

Hi,

with the following code

Code: Select all

  $NUM = 17.5;
  $strDays = format($NUM, "#,0.00");
  msg($NUM<br>$strDays);
I expected the output for $strDays to be "17,50" instead of "175,0".

No I wonder, if I simply haven't understood the style syntax.


Cheers,
Filehero


Edit: changed thread subject

- Moved to Bug Reports -

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: SC Format: number fraction handling?

Post by PeterH »

This belongs to strange :bug: , I'm afraid :shock:

Filehero
Posts: 2734
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: SC Format: number fraction handling?

Post by Filehero »

PeterH wrote:This belongs to strange :bug: , I'm afraid :shock:
Thanks for checking.

OK, maybe a mod can move it over to the bug forum.


Cheers,
Filehero

Edit: thanks mods for moving the thread. :)

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: SC Format: number fraction handling?

Post by Stefan »

Filehero wrote:Hi,

with the following code

Code: Select all

  $NUM = 17.5;
  $strDays = format($NUM, "#,0.00");
  msg($NUM<br>$strDays);
I expected the output for $strDays to be "17,50" instead of "175,0".


Please note that "#,0.00" is only an example, not the syntax to use.

Code: Select all

format()
Returns a string formatted.

Syntax
format(string, style)

string: String to format: can be a number or a date. 
style: Description of the desired format. 

Remarks: Date expressions should be formatted either in the current locale, or in the universal ISO 8601 format (YYYY-MM-DD hh:nn:ss).

Examples
text format(1, "000"); //001
text format(1, "@@@"); //  1 (leading spaces)
text format(1234.56, "#,0.00"); //1,234.56
text format(<date>, "yyyy"); //2010
text format(<date>, "dd-mmm-yy"); //20-Sep-10
text format(<date>, "dddd"); //Monday (in your locale)

As far as I understand, the syntax for number formatting is as follows:

The input 'string' must be formatted according to your current PC number setting as set in region setting.
The 'style' must be written with default English format: comma is thousand separator and dot is decimal separator.
The 'style' must not be used to change those number separators to format the output.
If in the input 'string', on the same position where you have put the '#' sign in the 'style', is an digit, then print that digit, else print nothing at that place.
If in the input 'string', on the same position where you have put the '0' in the 'style', is an digit, then print that digit, else, print a '0' at that place.



Example for German language setting:
   
$NUM = "17,5";
    $strDays = format($NUM, "00.00");
    msg("$NUM > $strDays"); >>> 17,5 > 17,50


$NUM = "17,5";
    $strDays = format($NUM, ".00");
    msg("$NUM > $strDays"); >>> 17,5 > 17,50


$NUM = "17,5";
    $strDays = format($NUM, "000.000");
    msg("$NUM > $strDays"); >>> 17,5 > 017,500


$NUM = "17,5";
    $strDays = format($NUM, "#00.0##");
    msg("$NUM > $strDays"); >>> 17,5 > 17,5


Pit falls:

$NUM = "17,5";
    $strDays = format($NUM, "000,00.00");
    msg("$NUM > $strDays"); >>> 17,5 > 00.017,50

Please note that '000,00.00' is not a correct formatted thousand, but the output is.
So the output looks wrong, but is correct as implemented.

And if I use a comma in the 'style' it's interpreted as
thousand separator in English, which is a dot in German:
$NUM = "17,5";
    $strDays = format($NUM, "000,000");
    msg("$NUM > $strDays"); >>> 17,5 > 000.018
Looks weird, but seams correct following the rules as that SC is coded.




Can someone confirm that?

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

Re: SC Format: number fraction handling?

Post by admin »

Yep, absolutely correct.

So if comma is your local decimal separator (as in Germany) the code should look like this:

Code: Select all

$NUM = "17,5";
  $strDays = format($NUM, "#,0.00");
  msg($NUM<br>$strDays);

However, there is still a problem. This does not work:

Code: Select all

$NUM = 35 / 2;
  $strDays = format($NUM, "#,0.00");
  msg($NUM<br>$strDays);
I will have to look at this...

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: SC Format: number fraction handling?

Post by PeterH »

:oops: Oh - I've just copied from the examples for testing, and missed the change to ",".

But interesting: in German locale 35/2 does the same: it leads to 17.5 :-)
So this seams to be the reason for your negative test...

By the way: for German locale the examples in help are a bit of misleading: changing them to use decimal comma like
text format(1234,56, "#,0.00"); //1,234.56
interprets the locale decimal-comma as an operand divider :-(
Of course this is formally correct - and I have no idea how to make (or explain) this better...

Edit
this means: in German locale (with decimal comma) it's not allowed in specifying numeric values for functions to use a decimal fraction, as the comma will be interpreted as a operand separator.
text(1,2);
OK: you can quote 1,2 - but I usually say numbers should not be quoted :roll:
So I must learn: in German numbers with fractions inside operand lists must be quoted :?

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

Re: SC Format: number fraction handling?

Post by admin »

There are a couple of fixes coming...

Filehero
Posts: 2734
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: SC Format: number fraction handling?

Post by Filehero »

admin wrote:However, there is still a problem. This does not work:

Code: Select all

$NUM = 35 / 2;
  $strDays = format($NUM, "#,0.00");
  msg($NUM<br>$strDays);
Which is exactly the problem I initially described. I just want to normalize the rendition of some math ops.

Btw, this is the output of the code above here on my German Windows 8.1 installation.
Test.png

Cheers,
Filehero
To see the attached files, you need to log into the forum.

Filehero
Posts: 2734
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: SC Format: number fraction handling?

Post by Filehero »

Fixed, thanks. :D

BTW, I guess there's a redundant format in the relase notes.

Code: Select all

echo format(format("1,23")); 
Cheers,
Filehero

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

Re: SC Format: number fraction handling?

Post by admin »

Partly, yes. It was not redundant before v13.30.0006 (and that was part of the bug), but now it is (as it should have been before).

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: SC Format: number fraction handling?

Post by PeterH »

1. test:

Code: Select all

  $NUM = 17,5;
  • set
    $NUM
    17
    5

The result is 17 :veryconfused:

Can't I set a number with fraction :shock:

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

Re: SC Format: number fraction handling?

Post by admin »

Hmmm, correct, there is still a problem with German fractions. The bug is probably somewhere in Berlin... :)

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: SC Format: number fraction handling?

Post by PeterH »

2. test:

Code: Select all

  $NUM = 35 / 2;
  $strDays = format($NUM, "#,0.00");
  msg($NUM<br>$strDays);
1st stmt sets $NUM to 17.5 (note the dot!)
format() expects decimal comma - and interprets this as 175
result is 175,00

Filehero
Posts: 2734
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: SC Format: number fraction handling?

Post by Filehero »

PeterH wrote:2. test:

Code: Select all

  $NUM = 35 / 2;
  $strDays = format($NUM, "#,0.00");
  msg($NUM<br>$strDays);
1st stmt sets $NUM to 17.5 (note the dot!)
Not here:
Test.png
with Windows 8.1

Code: Select all

OS: Windows 8 Professional with Media Center, 64-bit (6.2)

System Locale ID: 1031
Thread Locale ID: 1031
Default ANSI Code Page: 1252
Active Code Page: 1252  (ANSI - Lateinisch I)
DBCS Code Page: No
:?:


Cheers,
Filehero
To see the attached files, you need to log into the forum.

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

Re: SC Format: number fraction handling?

Post by admin »

PeterH wrote:2. test:

Code: Select all

  $NUM = 35 / 2;
  $strDays = format($NUM, "#,0.00");
  msg($NUM<br>$strDays);
1st stmt sets $NUM to 17.5 (note the dot!)
format() expects decimal comma - and interprets this as 175
result is 175,00
Not in v13.30.0006.

Post Reply