Package | flash.globalization |
Class | public final class NumberFormatter |
Inheritance | NumberFormatter Object |
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
int
,
uint
, and Number
objects.
The NumberFormatter class uses the data and functionality provided by the operating system and is designed to format numbers according to the conventions of a specific locale, based on the user's preferences and features supported by the user's operating system. The position of the negative symbol, the decimal separator, the grouping separator, the grouping pattern, and other elements within the number format can vary depending on the locale.
If the operating system supports the requested locale, the number formatting properties
are set according to the conventions and defaults of the requested locale.
If the requested locale is not available, then the properties are set according to
a fallback or default system locale, which can be retrieved using the actualLocaleIDName
property.
Due to the use of the user's settings, the use of formatting patterns provided by the operating system, and the use of a fallback locale when a requested locale is not supported, different users can see different formatting results, even when using the same locale ID.
Property | Defined By | ||
---|---|---|---|
actualLocaleIDName : String [read-only]
The name of the actual locale ID used by this NumberFormatter object. | NumberFormatter | ||
constructor : Object
A reference to the class object or constructor function for a given object instance. | Object | ||
decimalSeparator : String
The decimal separator character used for formatting or parsing numbers that have a decimal part. | NumberFormatter | ||
digitsType : uint
Defines the set of digit characters to be used when formatting numbers. | NumberFormatter | ||
fractionalDigits : int
The maximum number of digits that can appear after the decimal separator. | NumberFormatter | ||
groupingPattern : String
Describes the placement of grouping separators within the formatted number string. | NumberFormatter | ||
groupingSeparator : String
The character or string used for the grouping separator. | NumberFormatter | ||
lastOperationStatus : String [read-only]
The status of previous operation that this NumberFormatter object performed. | NumberFormatter | ||
leadingZero : Boolean
Specifies whether a leading zero is included in a formatted number when there are no integer digits
to the left of the decimal separator. | NumberFormatter | ||
negativeNumberFormat : uint
A numeric value that indicates a formatting pattern for negative numbers. | NumberFormatter | ||
negativeSymbol : String
The negative symbol to be used when formatting negative values. | NumberFormatter | ||
prototype : Object [static]
A reference to the prototype object of a class or function object. | Object | ||
requestedLocaleIDName : String [read-only]
The name of the requested locale ID that was passed to the constructor of this NumberFormatter object. | NumberFormatter | ||
trailingZeros : Boolean
Specifies whether trailing zeros are included in a formatted number. | NumberFormatter | ||
useGrouping : Boolean
Enables the use of the grouping separator when formatting numbers. | NumberFormatter |
Method | Defined By | ||
---|---|---|---|
NumberFormatter(requestedLocaleIDName:String)
Constructs a new NumberFormatter object to format numbers according to
the conventions of a given locale. | NumberFormatter | ||
Formats an int value. | NumberFormatter | ||
Formats a Number value. | NumberFormatter | ||
Formats a uint value. | NumberFormatter | ||
[static]
Lists all of the locale ID names supported by this class. | NumberFormatter | ||
Indicates whether an object has a specified property defined. | Object | ||
Indicates whether an instance of the Object class is in the prototype chain of the object specified
as the parameter. | Object | ||
Parses a string and returns a NumberParseResult object containing the parsed elements. | NumberFormatter | ||
Parses a string that contains only digits and optional whitespace characters and returns a Number. | NumberFormatter | ||
Indicates whether the specified property exists and is enumerable. | Object | ||
Sets the availability of a dynamic property for loop operations. | Object | ||
Returns the string representation of this object, formatted according to locale-specific conventions. | Object | ||
Returns the string representation of the specified object. | Object | ||
Returns the primitive value of the specified object. | Object |
actualLocaleIDName | property |
actualLocaleIDName:String
[read-only] Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
The name of the actual locale ID used by this NumberFormatter object.
There are three possibilities for the value of the name, depending on operating system and the
value of the requestedLocaleIDName
parameter passed to the Collator()
constructor.
- If the requested locale was not
LocaleID.DEFAULT
and the operating system provides support for the requested locale, then the name returned is the same as therequestedLocaleIDName
property. - If
LocaleID.DEFAULT
was used as the value for therequestedLocaleIDName
parameter to the constructor, then the name of the current locale specified by the user's operating system is used. TheLocaleID.DEFAULT
value preserves user's customized setting in the OS. Passing an explicit value as therequestedLocaleIDName
parameter does not necessarily give the same result as using theLocaleID.DEFAULT
even if the two locale ID names are the same. The user could have customized the locale settings on their machine, and by requesting an explicit locale ID name rather than usingLocaleID.DEFAULT
your application would not retrieve those customized settings. - If the system does not support the
requestedLocaleIDName
specified in the constructor then a fallback locale ID name is provided.
Implementation
public function get actualLocaleIDName():String
Related API Elements
decimalSeparator | property |
decimalSeparator:String
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
The decimal separator character used for formatting or parsing numbers that have a decimal part.
This property is initially set based on the locale that is selected when the formatter object is constructed.
When this property is assigned a value and there are no errors or warnings, the lastOperationStatus
property
is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
The default value is dependent on the locale and operating system..
Implementation
public function get decimalSeparator():String
public function set decimalSeparator(value:String):void
Throws
TypeError — if this property is assigned a null value.
|
Related API Elements
digitsType | property |
digitsType:uint
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
Defines the set of digit characters to be used when formatting numbers.
Different languages and regions use different sets of characters to represent the digits 0 through 9. This property defines the set of digits to be used.
The value of this property represents the Unicode value for the zero digit of a decimal digit set. The valid values for this property are defined in the NationalDigitsType class.
When this property is assigned a value and there are no errors or warnings, the lastOperationStatus
property
is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
The default value is dependent on the locale and operating system..
Implementation
public function get digitsType():uint
public function set digitsType(value:uint):void
Throws
TypeError — if this property is assigned a null value.
|
Related API Elements
fractionalDigits | property |
fractionalDigits:int
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
The maximum number of digits that can appear after the decimal separator.
Numbers are rounded to the number of digits specified by this property. The rounding scheme varies depending on the user's operating system.
When the trailingZeros
property is set to true
, the fractional portion of the
number (after the decimal point) is padded with trailing zeros until its length matches the value of this
fractionalDigits
property.
When this property is assigned a value and there are no errors or warnings, the lastOperationStatus
property
is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
The default value is 0.
Implementation
public function get fractionalDigits():int
public function set fractionalDigits(value:int):void
Related API Elements
groupingPattern | property |
groupingPattern:String
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
Describes the placement of grouping separators within the formatted number string.
When the useGrouping
property is set to true, the groupingPattern
property is used
to define the placement and pattern used for the grouping separator.
The grouping pattern is defined as a string containing numbers separated by semicolons and optionally may end
with an asterisk. For example: "3;2;*"
. Each number in the string represents the number of digits
in a group. The grouping separator is placed before each group of digits. An asterisk at the end of the string
indicates that groups with that number of digits should be repeated for the rest of the formatted string.
If there is no asterisk then there are no additional groups or separators for the rest of the formatted string.
The first number in the string corresponds to the first group of digits to the left of the decimal separator.
Subsequent numbers define the number of digits in subsequent groups to the left. Thus the string "3;2;*"
indicates that a grouping separator is placed after the first group of 3 digits, followed by groups of 2 digits.
For example: 98,76,54,321
The following table shows examples of formatting the number 123456789.12 with various grouping patterns. The grouping separator is a comma and the decimal separator is a period.
Grouping Pattern | Sample Format |
3;* | 123,456,789.12 |
3;2;* | 12,34,56,789.12 |
3 | 123456,789.12 |
Only a limited number of grouping sizes can be defined. On some operating systems, grouping patterns can only contain
two numbers plus an asterisk. Other operating systems can support up to four numbers and an asterisk.
For patterns without an asterisk, some operating systems only support one number while others support up to three numbers.
If the maximum number of grouping pattern elements is exceeded, then additional elements
are ignored and the lastOperationStatus
property is set as described below.
When this property is assigned a value and there are no errors or warnings, the lastOperationStatus
property
is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
Implementation
public function get groupingPattern():String
public function set groupingPattern(value:String):void
Throws
TypeError — if this property is assigned a null value.
|
Related API Elements
groupingSeparator | property |
groupingSeparator:String
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
The character or string used for the grouping separator.
The value of this property is used as the grouping separator when formatting numbers with the
useGrouping
property set to true
. This
property is initially set based on the locale that is selected when the formatter object
is constructed.
When this property is assigned a value and there are no errors or warnings, the lastOperationStatus
property
is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
The default value is dependent on the locale and operating system..
Implementation
public function get groupingSeparator():String
public function set groupingSeparator(value:String):void
Throws
TypeError — if this property is assigned a null value.
|
Related API Elements
lastOperationStatus | property |
lastOperationStatus:String
[read-only] Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
The status of previous operation that this NumberFormatter object performed.
The lastOperationStatus
property is set whenever the constructor or a method of
this class is called, or another property is set. For the possible values see the description for each method.
Implementation
public function get lastOperationStatus():String
Related API Elements
leadingZero | property |
leadingZero:Boolean
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
Specifies whether a leading zero is included in a formatted number when there are no integer digits to the left of the decimal separator.
When this property is set to true
a leading zero is included to the left of the decimal separator
when formatting numeric values between -1.0 and 1.0.
When this property is set to false
a leading zero is not included.
For example if the number is 0.321 and this property is set true
, then the leading
zero is included in the formatted string. If the property is set to false
, the leading zero
is not included. In that case the string would just include the decimal separator followed by the decimal digits,
like .321
.
The following table shows examples of how numbers are formatted based on the values of this property and
the related fractionalDigits
and trailingZeros
properties.
trailingZeros | leadingZero | fractionalDigits | 0.12 | 0 |
true | true | 3 | 0.120 | 0.000 |
false | true | 3 | 0.12 | 0 |
true | false | 3 | .120 | .000 |
false | false | 3 | .12 | 0 |
When this property is assigned a value and there are no errors or warnings, the lastOperationStatus
property
is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
The default value is dependent on the locale and operating system..
Implementation
public function get leadingZero():Boolean
public function set leadingZero(value:Boolean):void
Throws
TypeError — if this property is assigned a null value.
|
Related API Elements
negativeNumberFormat | property |
negativeNumberFormat:uint
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
A numeric value that indicates a formatting pattern for negative numbers. This pattern defines the location of the negative symbol or parentheses in relation to the numeric portion of the formatted number.
The following table summarizes the possible formats for negative numbers. When a negative number is formatted,
the minus sign in the format is replaced with the value of
the negativeSymbol
property and the 'n' character is replaced with the formatted numeric value.
Negative number format type | Format |
0 | (n) |
1 | -n |
2 | - n |
3 | n- |
4 | n - |
When this property is assigned a value and there are no errors or warnings, the lastOperationStatus
property
is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
The default value is dependent on the locale and operating system..
Implementation
public function get negativeNumberFormat():uint
public function set negativeNumberFormat(value:uint):void
Throws
ArgumentError — if the assigned value is not a number between 0 and 4.
|
Related API Elements
negativeSymbol | property |
negativeSymbol:String
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
The negative symbol to be used when formatting negative values.
This symbol is used with the negative number format when formatting a number that is less than zero. It is not used in negative number formats that do not include a negative sign (e.g. when negative numbers are enclosed in parentheses).
This property is set to a default value for the actual locale selected when this formatter is constructed. It can be set with a value to override the default setting.
When this property is assigned a value and there are no errors or warnings, the lastOperationStatus
property
is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
Implementation
public function get negativeSymbol():String
public function set negativeSymbol(value:String):void
Throws
MemoryError — if the system cannot allocate enough internal memory.
|
Related API Elements
requestedLocaleIDName | property |
requestedLocaleIDName:String
[read-only] Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
The name of the requested locale ID that was passed to the constructor of this NumberFormatter object.
If the LocaleID.DEFAULT
value was used then the name returned is "i-default".
The actual locale used can differ from the requested locale when a fallback locale is applied.
The name of the actual locale can be retrieved using the actualLocaleIDName
property.
Implementation
public function get requestedLocaleIDName():String
Related API Elements
trailingZeros | property |
trailingZeros:Boolean
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
Specifies whether trailing zeros are included in a formatted number.
When this property is set to true
, trailing zeros are included in the fractional part
of the formatted number up to the limit specified by the fractionalDigits
property.
When this property is set to false
then no trailing zeros are shown.
For example if the numeric value is 123.4, and this property is set true, and the fractionalDigits
property
is set to 3, the formatted string would show trailing zeros, like 123.400
.
If this property is false
, trailing zeros are not included, and the string shows just the decimal
separator followed by the non-zero decimal digits, like 123.4
.
The following table shows examples of how numeric values are formatted based on the values of this property and
the related fractionalDigits
and leadingZero
properties.
trailingZeros | leadingZero | fractionalDigits | 0.12 | 0 |
true | true | 3 | 0.120 | 0.000 |
false | true | 3 | 0.12 | 0 |
true | false | 3 | .120 | .000 |
false | false | 3 | .12 | 0 |
When this property is assigned a value and there are no errors or warnings, the lastOperationStatus
property
is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
The default value is dependent on the locale and operating system..
Implementation
public function get trailingZeros():Boolean
public function set trailingZeros(value:Boolean):void
Throws
TypeError — if this property is assigned a null value.
|
Related API Elements
useGrouping | property |
useGrouping:Boolean
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
Enables the use of the grouping separator when formatting numbers.
When the useGrouping
property is set to true
, digits are grouped and
delimited by the grouping separator character.
For example: 123,456,789.22
When the useGrouping
property is set to false
, digits are not grouped or separated.
For example: 123456789.22
The symbol to be used as a grouping separator is defined by the groupingSeparator
property. The number of digits
between grouping separators is defined by the groupingPattern
property.
When this property is assigned a value and there are no errors or warnings, the lastOperationStatus
property
is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
Implementation
public function get useGrouping():Boolean
public function set useGrouping(value:Boolean):void
Related API Elements
NumberFormatter | () | Constructor |
public function NumberFormatter(requestedLocaleIDName:String)
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
Constructs a new NumberFormatter object to format numbers according to the conventions of a given locale.
This constructor determines if the current operating system supports the requested locale ID name.
If it is not supported then a fallback locale is used instead.
If a fallback locale is used then the the lastOperationStatus
property
indicates the type of fallback, and the actualLocaleIDName
property contains
the name of the fallback locale ID.
To format based on the user's current operating system preferences, pass the value LocaleID.DEFAULT
in the requestedLocaleIDName
parameter to the constructor.
When the constructor completes successfully, the lastOperationStatus
property is set to:
LastOperationStatus.NO_ERROR
When the requested locale ID name is not available then the lastOperationStatus
is set to one of the following:
LastOperationStatus.USING_FALLBACK_WARNING
LastOperationStatus.USING_DEFAULT_WARNING
If this class is not supported on the current operating system, then the lastOperationStatus
property is set to:
LastOperationStatus.UNSUPPORTED_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in
the LastOperationStatus class.
For details on the warnings listed above and other possible values of the
lastOperationStatus
property see the descriptions in the LastOperationStatus
class.
requestedLocaleIDName:String — The preferred locale ID name to use when determining number formats.
|
Throws
TypeError — if the requestedLocaleIDName is null
|
Related API Elements
formatInt | () | method |
public function formatInt(value:int):String
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
Formats an int value.
This function is equivalent to the formatNumber()
method except that it takes an int
value.
If the value passed in is too large or small, such as a value greater than 1.72e308 or less than 1.72e-308, then this function returns 0.
When this method is called and it completes successfully, the lastOperationStatus
property is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
Parameters
value:int — An int value to format.
|
String — A formatted number string.
|
Throws
MemoryError — for any internal memory allocation problems.
|
Related API Elements
formatNumber | () | method |
public function formatNumber(value:Number):String
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
Formats a Number value.
This function formats the number based on the property values of the formatter. If the properties are not modified after the the numberFormatter object is created, the numbers are formatted according to the locale specific conventions provided by the operating system for the locale identified by actualLocaleIDName. To customize the format, the properties can be altered to control specific aspects of formatting a number.
Very large numbers and very small magnitude numbers can be formatted with this function. However, the number of significant digits is limited to the precision provided by the Number object. Scientific notation is not supported.
When this method is called and it completes successfully, the lastOperationStatus
property is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
Parameters
value:Number — A Number value to format.
|
String — A formatted number string.
|
Throws
MemoryError — if there are any internal memory allocation problems.
|
Related API Elements
formatUint | () | method |
public function formatUint(value:uint):String
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
Formats a uint value.
This function is equivalent to the formatNumber()
method except that it takes a uint
.
If the value passed in is too large, such as a value greater than 1.72e308, then this function returns 0.
When this method is called and it completes successfully, the lastOperationStatus
property is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
Parameters
value:uint — A uint value.
|
String — A formatted number string.
|
Throws
MemoryError — if there are any internal memory allocation problems.
|
Related API Elements
getAvailableLocaleIDNames | () | method |
public static function getAvailableLocaleIDNames():Vector.<String>
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
Lists all of the locale ID names supported by this class.
If this class is not supported on the current operating system, this method returns a null value.
When this method is called and it completes successfully, the lastOperationStatus
property is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
Vector.<String> — A vector of strings containing all of the locale ID names supported by this class.
|
parse | () | method |
public function parse(parseString:String):NumberParseResult
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
Parses a string and returns a NumberParseResult object containing the parsed elements.
The NumberParseResult object contains the value of the first number found in the input string, the starting index for the number within the string, and the index of the first character after the number in the string.
If the string does not contain a number, the value property of the NumberParseResult is set to NaN
and the
startIndex
and endIndex
properties are set to the hexadecimal value 0x7fffffff
.
This function uses the value of the decimalSeparator
property to determine the portion of the number
that contains fractional
digits, and the groupingSeparator
property to determine which characters are allowed within the digits of a number,
and the negativeNumberFormat
property to control how negative values are represented.
The following table identifies the result of strings parsed for the various NegativeNumberFormat values:
NegativeNumberFormat | Input String | Result |
(n) | "(123)" or "( 123 )" | "-123" |
-n | "-123" or "- 123" | "-123" |
- n | "-123" or "- 123" | "-123" |
n- | "123-" or "123 -" | "-123" |
n - | "123-" or "123 -" | "-123" |
A single white space is allowed between the number and the minus sign or parenthesis.
Other properties are ignored when determining a valid number. Specifically the value of the
digitsType
property is ignored and the digits can be from any of
the digit sets that are enumerated in the NationalDigitsType class. The values of the groupingPattern
and useGrouping
properties do not influence the parsing of the number.
If numbers are preceded or followed in the string by a plus sign '+', the plus sign is treated as a character that is not part of the number.
This function does not parse strings containing numbers in scientific notation (e.g. 1.23e40).
When this method is called and it completes successfully, the lastOperationStatus
property is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
Parameters
parseString:String |
NumberParseResult |
Throws
TypeError — if the parseString is null
|
Related API Elements
Example ( How to use this example )
var nf:NumberFormatter = new NumberFormatter("fr-FR"); var str:String = "1,56 mètre" var result:NumberParseResult = nf.parse(str); trace(result.value) // 1.56 trace(str.substr(0,result.startIndex)); // "" trace(str.substr(result.startIndex, result.endIndex)); // "1,56" trace(str.substr(result.endIndex)); // " mètre"
parseNumber | () | method |
public function parseNumber(parseString:String):Number
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 10.1, AIR 2 |
Parses a string that contains only digits and optional whitespace characters and returns a Number.
If the string does not begin
with a number or contains characters other than whitespace that are not part of the number, then
this method returns NaN
. White space before or after the numeric digits is ignored. A white space
character is a character that has a Space Separator (Zs) property in the Unicode Character Database (see http://www.unicode.org/ucd/).
If the numeric digit is preceded or followed by a plus sign '+' it is treated as a non-whitespace character.
The return value is NaN
.
See the description of the parse function for more information about number parsing and what constitutes a valid number.
When this method is called and it completes successfully, the lastOperationStatus
property is set to:
LastOperationStatus.NO_ERROR
Otherwise the lastOperationStatus
property is set to one of the constants defined in the
LastOperationStatus
class.
Parameters
parseString:String |
Number |
Throws
TypeError — if the parseString is null
|
Related API Elements
This example uses the following locales: default operating system locale for number formatting, Japanese (Japan), English (US), and
French (France). The example uses the static member LocaleID.DEFAULT
to request the default operating system locale.
The results from this example might differ based on your operating system and user preferences.
This example does the following for each locale in the list:
- Creates a NumberFormatter object.
- Formats the same value as a Number, an integer, and an unsigned integer, and displays the results.
package { import flash.globalization.NumberFormatter; import flash.globalization.LocaleID; public class NumberFormatterExample extends Sprite { public function NumberFormatterExample():void { var localeNames:Array = [LocaleID.DEFAULT,"ja_JP","en_US","fr_FR"]; for ( var i:int = 0; i < localeNames.length; i++ ) { var nf:NumberFormatter = new NumberFormatter( localeNames[i] as String ); trace('\n\n' + "LocaleID requested: " + nf.requestedLocaleIDName + "; actual: " + nf.actualLocaleIDName); trace( "Last Operation Status:" + nf.lastOperationStatus ); var numberString:String = nf.formatNumber(123456789.19); trace( "Formatted Number:" + numberString); numberString = nf.formatInt(-123456789); trace( "Formatted Int:" + numberString); numberString = nf.formatUint(123456789); trace( "Formatted UInt:" + numberString); } } } }
The results from this example might differ based on your operating system and user preferences.
This example does the following:
- Creates a NumberFormatter object.
- Calls the
NumberFormatter.parse()
method to parse the string and return a NumberParseResult object. - Calls the
NumberFormatter.parseNumber()
method to parse the string and return a Number value.
package { import flash.globalization.NumberFormatter; import flash.globalization.NumberParseResult; import flash.globalization.LastOperationStatus; import flash.globalization.LocaleID; public class NumberFormatterParseExample { public function NumberFormatterParseExample():void { var nf:NumberFormatter = new NumberFormatter( "en_US" ); trace("LocaleID requested: " + nf.requestedLocaleIDName + "; actual: " + nf.actualLocaleIDName); trace( "Last Operation Status:" + nf.lastOperationStatus ); var inputNumberString:String = "123,567,89,0.254"; var parseResult:NumberParseResult = nf.parse(inputNumberString); if ( nf.lastOperationStatus == LastOperationStatus.NO_ERROR ) { trace("Parsed value:" + parseResult.value); } inputNumberString = "-123,567,89,0.254"; var parsedNumber:Number = nf.parseNumber(inputNumberString); if ( nf.lastOperationStatus == LastOperationStatus.NO_ERROR ) { trace("Parsed value:" + parsedNumber); } } } }
Mon Nov 28 2011, 06:48 AM -08:00