Page 1 of 3

SC Format: number fraction handling?

Posted: 16 Nov 2013 12:33
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 -

Re: SC Format: number fraction handling?

Posted: 16 Nov 2013 17:29
by PeterH
This belongs to strange :bug: , I'm afraid :shock:

Re: SC Format: number fraction handling?

Posted: 16 Nov 2013 18:43
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. :)

Re: SC Format: number fraction handling?

Posted: 16 Nov 2013 23:38
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?

Re: SC Format: number fraction handling?

Posted: 17 Nov 2013 09:22
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...

Re: SC Format: number fraction handling?

Posted: 17 Nov 2013 11:56
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 :?

Re: SC Format: number fraction handling?

Posted: 17 Nov 2013 12:22
by admin
There are a couple of fixes coming...

Re: SC Format: number fraction handling?

Posted: 17 Nov 2013 12:27
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

Re: SC Format: number fraction handling?

Posted: 17 Nov 2013 13:30
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

Re: SC Format: number fraction handling?

Posted: 17 Nov 2013 13:36
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).

Re: SC Format: number fraction handling?

Posted: 17 Nov 2013 14:42
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:

Re: SC Format: number fraction handling?

Posted: 17 Nov 2013 14:46
by admin
Hmmm, correct, there is still a problem with German fractions. The bug is probably somewhere in Berlin... :)

Re: SC Format: number fraction handling?

Posted: 17 Nov 2013 14:51
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

Re: SC Format: number fraction handling?

Posted: 17 Nov 2013 19:57
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

Re: SC Format: number fraction handling?

Posted: 17 Nov 2013 20:22
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.