Packagemx.formatters
Classpublic class CurrencyFormatter
InheritanceCurrencyFormatter Inheritance Formatter Inheritance Object

The CurrencyFormatter class formats a valid number as a currency value. It adjusts the decimal rounding and precision, the thousands separator, and the negative sign; it also adds a currency symbol. You place the currency symbol on either the left or the right side of the value with the alignSymbol property. The currency symbol can contain multiple characters, including blank spaces.

If an error occurs, an empty String is returned and a String that describes the error is saved to the error property. The error property can have one of the following values:

MXML Syntaxexpanded Hide MXML Syntax

The <mx:CurrencyFormatter> tag inherits all of the tag attributes of its superclass, and adds the following tag attributes:

  <mx:CurrencyFormatter
    alignSymbol="left|right" 
    currencySymbol="$"
    decimalSeparatorFrom="."
    decimalSeparatorTo="."
    precision="-1"
    rounding="none|up|down|nearest"
    thousandsSeparatorFrom=","
    thousandsSeparatorTo=","
    useNegativeSign="true|false"
    useThousandsSeparator="true|false"
 />  
  

View the examples.

See also

mx.formatters.NumberBase
mx.formatters.NumberBaseRoundType


Public Properties
 PropertyDefined by
  alignSymbol : String
Aligns currency symbol to the left side or the right side of the formatted number.
CurrencyFormatter
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
  currencySymbol : String
Character to use as a currency symbol for a formatted number.
CurrencyFormatter
  decimalSeparatorFrom : String
Decimal separator character to use when parsing an input string.
CurrencyFormatter
  decimalSeparatorTo : String
Decimal separator character to use when outputting formatted decimal numbers.
CurrencyFormatter
 Inheritederror : String
Description saved by the formatter when an error occurs.
Formatter
  precision : int
Number of decimal places to include in the output String.
CurrencyFormatter
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
  rounding : String
How to round the number.
CurrencyFormatter
  thousandsSeparatorFrom : String
Character to use as the thousands separator in the input String.
CurrencyFormatter
  thousandsSeparatorTo : String
Character to use as the thousands separator in the output string.
CurrencyFormatter
  useNegativeSign : Boolean
If true, format a negative number by preceding it with a minus "-" sign.
CurrencyFormatter
  useThousandsSeparator : Boolean
If true, split the number into thousands increments by using a separator character.
CurrencyFormatter
Protected Properties
 PropertyDefined by
 InheriteddefaultInvalidFormatError : String = "Invalid format"
Error message for an invalid format string specified to the formatter.
Formatter
 InheriteddefaultInvalidValueError : String = "Invalid value"
Error message for an invalid value specified to the formatter.
Formatter
 InheritedpackageResources : ResourceBundle
[static] A ResourceBundle object containing all symbols from formatters.properties.
Formatter
Public Methods
 MethodDefined by
  
Constructor.
CurrencyFormatter
  
Formats value as currency.
CurrencyFormatter
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
Property detail
alignSymbolproperty
public var alignSymbol:String

Aligns currency symbol to the left side or the right side of the formatted number. Permitted values are "left" and "right".

The default value is "left".

currencySymbolproperty 
public var currencySymbol:String

Character to use as a currency symbol for a formatted number. You can use one or more characters to represent the currency symbol; for example, "$" or "YEN". You can also use empty spaces to add space between the currency character and the formatted number. When the number is a negative value, the currency symbol appears between the number and the minus sign or parentheses.

The default value is "$".

decimalSeparatorFromproperty 
public var decimalSeparatorFrom:String

Decimal separator character to use when parsing an input string.

The default value is ".".

decimalSeparatorToproperty 
public var decimalSeparatorTo:String

Decimal separator character to use when outputting formatted decimal numbers.

The default value is ".".

precisionproperty 
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 1.453. If precision is -1 and you set some form of rounding, return a value based on that rounding type.

The default value is -1.

roundingproperty 
public var rounding:String

How to round the number. In ActionScript, the value can be NumberBaseRoundType.NONE, NumberBaseRoundType.UP, NumberBaseRoundType.DOWN, or NumberBaseRoundType.NEAREST. In MXML, the value can be "none", "up", "down", or "nearest".

The default value is NumberBaseRoundType.NONE.

See also

thousandsSeparatorFromproperty 
public var thousandsSeparatorFrom:String

Character to use as the thousands separator in the input String.

The default value is ",".

thousandsSeparatorToproperty 
public var thousandsSeparatorTo:String

Character to use as the thousands separator in the output string.

The default value is ",".

useNegativeSignproperty 
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.

useThousandsSeparatorproperty 
public var useThousandsSeparator:Boolean

If true, split the number into thousands increments by using a separator character.

The default value is true.

Constructor detail
CurrencyFormatter()constructor
public function CurrencyFormatter()

Constructor.

Method detail
format()method
public override function format(value:Object):String

Formats value as currency. If value cannot be formatted, return an empty String and write a description of the error to the error property.

Parameters
value:Object — Value to format.

Returns
String — Formatted string. Empty if an error occurs.
Examples
CurrencyFormatterExample
<?xml version="1.0" encoding="utf-8"?>
<!-- Simple example to demonstrate the CurrencyFormatter. -->
<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) {
                        var temp:Number=Number(priceUS.text); 
                        formattedUSPrice.text= usdFormatter.format(temp);
                    }
                    
                    else {
                       formattedUSPrice.text="";
                    }
              }
        ]]>
    </mx:Script>

    <mx:CurrencyFormatter id="usdFormatter" precision="2" 
        currencySymbol="$" decimalSeparatorFrom="."
        decimalSeparatorTo="." useNegativeSign="true" 
        useThousandsSeparator="true" alignSymbol="left"/>

    <mx:NumberValidator id="numVal" source="{priceUS}" property="text" 
        allowNegative="true" domain="real"/>

    <mx:Panel title="CurrencyFormatter Example" width="75%" height="75%" 
        paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">

        <mx:Form>
            <mx:FormItem label="Enter U.S. dollar amount:">
                <mx:TextInput id="priceUS" text="" width="50%"/>
            </mx:FormItem>

            <mx:FormItem label="Formatted amount: ">
                <mx:TextInput id="formattedUSPrice" text="" width="50%" editable="false"/>
            </mx:FormItem>

            <mx:FormItem>
                <mx:Button label="Validate and Format" click="Format();"/>
            </mx:FormItem>
        </mx:Form>

    </mx:Panel>
</mx:Application>




Take a survey