The NumberFormatter class formats a valid number by adjusting the decimal rounding and precision, the thousands separator, and the negative sign.
If you use both the rounding
and precision
properties, rounding is applied first, and then you set the decimal length
by using the specified precision
value.
This lets you round a number and still have a trailing decimal;
for example, 303.99 = 304.00.
If an error occurs, an empty String is returned and a String
describing the error is saved to the error
property.
The error
property can have one of the following values:
"Invalid value"
means an invalid numeric value is passed to theformat()
method. The value should be a valid number in the form of a Number or a String."Invalid format"
means one of the parameters contain an unusable setting.
See also:
Constructor
Variables
decimalSeparatorFrom:String
Decimal separator character to use when parsing an input String.
When setting this property, ensure that the value of the
thousandsSeparatorFrom
property does not equal this property.
Otherwise, an error occurs when formatting the value.
decimalSeparatorTo:String
Decimal separator character to use when outputting formatted decimal numbers.
When setting this property, ensure that the value of the
thousandsSeparatorTo
property does not equal this property.
Otherwise, an error occurs when formatting the value.
precision:Int
Number of decimal places to include in the output String.
You can disable precision by setting it to -1
.
A value of -1
means do not change the precision. For example,
if the input value is 1.453 and rounding
is set to NumberBaseRoundType.NONE
, return a value of 1.453.
If precision
is -1
and you have set some form of
rounding, return a value based on that rounding type.
rounding:String
Specifies how to round the number.
In Haxe, you can use the following constants to set this property:
See also:
thousandsSeparatorFrom:String
Character to use as the thousands separator in the input String.
When setting this property, ensure that the value of the
decimalSeparatorFrom
property does not equal this property.
Otherwise, an error occurs when formatting the value.
thousandsSeparatorTo:String
Character to use as the thousands separator in the output String.
When setting this property, ensure that the value of the
decimalSeparatorTo
property does not equal this property.
Otherwise, an error occurs when formatting the value.
useNegativeSign:Bool
If true
, format a negative number
by preceding it with a minus "-" sign.
If false
, format the number
surrounded by parentheses, for example (400).
useThousandsSeparator:Bool
If true
, split the number into thousands increments
by using a separator character.
Methods
format(value:Dynamic):String
Formats the number as a String.
If value
cannot be formatted, return an empty String
and write a description of the error to the error
property.
Parameters:
value | Value to format. |
---|
Returns:
Formatted String. Empty if an error occurs.