| Package | mx.formatters |
| Class | public class NumberFormatter |
| Inheritance | NumberFormatter Formatter Object |
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
the format() 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.
Hide MXML Syntax
The <mx:NumberFormatter> tag
inherits all of the tag attributes of its superclass,
and adds the following tag attributes:
<mx:NumberFormatter
decimalSeparatorFrom="."
decimalSeparatorTo="."
precision="-1"
rounding="none|up|down|nearest"
thousandsSeparatorFrom=","
thousandsSeparatorTo=","
useNegativeSign="true|false"
useThousandsSeparator="true|false"/>
See also
| Property | Defined by | ||
|---|---|---|---|
![]() | constructor : Object
A reference to the class object or constructor function for a given object instance.
| Object | |
| decimalSeparatorFrom : String
Decimal separator character to use
when parsing an input String.
| NumberFormatter | ||
| decimalSeparatorTo : String
Decimal separator character to use
when outputting formatted decimal numbers.
| NumberFormatter | ||
![]() | error : String
Description saved by the formatter when an error occurs.
| Formatter | |
| precision : int
Number of decimal places to include in the output String.
| NumberFormatter | ||
![]() | prototype : Object
[static]
A reference to the prototype object of a class or function object.
| Object | |
| rounding : String
Specifies how to round the number.
| NumberFormatter | ||
| thousandsSeparatorFrom : String
Character to use as the thousands separator
in the input String.
| NumberFormatter | ||
| thousandsSeparatorTo : String
Character to use as the thousands separator
in the output String.
| NumberFormatter | ||
| useNegativeSign : Boolean
If
true, format a negative number
by preceding it with a minus "-" sign. | NumberFormatter | ||
| useThousandsSeparator : Boolean
If
true, split the number into thousands increments
by using a separator character. | NumberFormatter | ||
| Method | Defined by | ||
|---|---|---|---|
|
Constructor.
| NumberFormatter | ||
|
Formats the number as a String.
| NumberFormatter | ||
![]() |
Sets the availability of a dynamic property for loop operations.
| Object | |
![]() |
Returns the string representation of the specified object.
| Object | |
![]() |
Returns the primitive value of the specified object.
| Object | |
| decimalSeparatorFrom | property |
public var decimalSeparatorFrom:StringDecimal separator character to use when parsing an input String.
The default value is ".".
| decimalSeparatorTo | property |
public var decimalSeparatorTo:StringDecimal separator character to use when outputting formatted decimal numbers.
The default value is ".".
| precision | property |
public var 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.
The default value is -1.
| rounding | property |
public var rounding:StringSpecifies how to round the number.
In ActionScript, you can use the following constants to set this property:
NumberBaseRoundType.NONE, NumberBaseRoundType.UP,
NumberBaseRoundType.DOWN, or NumberBaseRoundType.NEAREST.
Valid MXML values are "down", "nearest", "up", and "none".
The default value is NumberBaseRoundType.NONE.
See also
| thousandsSeparatorFrom | property |
public var thousandsSeparatorFrom:StringCharacter to use as the thousands separator in the input String.
The default value is ",".
| thousandsSeparatorTo | property |
public var thousandsSeparatorTo:StringCharacter to use as the thousands separator in the output String.
The default value is ",".
| useNegativeSign | property |
public var useNegativeSign:Boolean
If true, format a negative number
by preceding it with a minus "-" sign.
If false, format the number
surrounded by parentheses, for example (400).
The default value is true.
| useThousandsSeparator | property |
public var useThousandsSeparator:Boolean
If true, split the number into thousands increments
by using a separator character.
The default value is true.
| NumberFormatter | () | constructor |
public function NumberFormatter()Constructor.
| format | () | method |
public override function format(value:Object):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.
value:Object — Value to format.
|
String —
Formatted String. Empty if an error occurs.
|
<?xml version="1.0"?>
<!-- Simple example to demonstrate NumberFormatter. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.events.ValidationResultEvent;
private var vResult:ValidationResultEvent;
// Event handler to validate and format input.
private function Format():void
{
vResult = numVal.validate();
if (vResult.type==ValidationResultEvent.VALID) {
formattedNumber.text= numberFormatter.format(inputVal.text);
}
else {
formattedNumber.text= "";
}
}
]]>
</mx:Script>
<mx:NumberFormatter id="numberFormatter" precision="4"
useThousandsSeparator="true" useNegativeSign="true"/>
<mx:NumberValidator id="numVal" source="{inputVal}" property="text"
allowNegative="true" domain="real"/>
<mx:Panel title="NumberFormatter Example" width="75%" height="75%"
paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">
<mx:Form>
<mx:FormItem label="Enter number:">
<mx:TextInput id="inputVal" text="" width="50%"/>
</mx:FormItem>
<mx:FormItem label="Formatted number (precision=4): ">
<mx:TextInput id="formattedNumber" editable="false" width="50%"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button label="Validate and Format" click="Format();"/>
</mx:FormItem>
</mx:Form>
</mx:Panel>
</mx:Application>