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?