Formatter.Decimal (Decimal Formatter)
An instance of the Formatter.Decimal class (Decimal Formatter) is used to display numeric data. This formatter class formats and parses Decimal-valued strings, not Number values.
Here’s an example of creating a new Decimal instance from a Number:
New Decimal Object
// Decimal from Numeric String
dVal = Decimal.fromString("12345")
// Decimal from Number
numericValue = 12345
dVal = Decimal.fromString(String(numericValue))
Decimal Formatter Class Properties
Here are the properties of the Formater.Decimal class:
currencyCodes (--> String r/o) • Deprecated: Please use the currencyCode property on the instance of the Locale class instead. Returns the list of known ISO currency codes.
custom (--> Formater.Decimal r/o) • Returns a new formatter that can be configured with custom settings.
decimal (--> Formater.Decimal r/o) • Returns a new number formatter that will use both a decimal separator.
percent (--> Formater.Decimal r/o) • Returns a new number formatter that will display the value as a percentage.
percentWithDecimal (--> Formater.Decimal r/o) • Returns a new number formatter that will display the value as a percentage with a decimal separator.
plain (--> Formater.Decimal r/o) • Returns a new number formatter that will not use any separators.
thousandsAndDecimal (--> Formater.Decimal r/o) • Returns a new number formatter that will use both a thousands and decimal separator.
Currency Codes Property (depricated)
Formatter.Decimal.currencyCodes
//--> ["ADP", "AED", "AFA", "AFN", "ALK", "ALL", "AMD", "ANG", "AOA", "AOK", "AON", "AOR", "ARA", "ARL", "ARM", "ARP", "ARS", "ATS", "AUD", "AWG", "AZM", "AZN", "BAD", "BAM", "BAN", "BBD", "BDT", "BEC", "BEF", "BEL", "BGL", "BGM", "BGN", "BGO", "BHD", "BIF", "BMD", "BND", "BOB", "BOL", "BOP", "BOV", "BRB", "BRC", "BRE", "BRL", "BRN", "BRR", "BRZ", "BSD", "BTN", "BUK", "BWP", "BYB", "BYN", "BYR", "BZD", "CAD", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "CNH", "CNX", "CNY", "COP", "COU", "CRC", "CSD", "CSK", "CUC", "CUP", "CVE", "CYP", "CZK", "DDM", "DEM", "DJF", "DKK", "DOP", "DZD", "ECS", "ECV", "EEK", "EGP", "EQE", "ERN", "ESA", "ESB", "ESP", "ETB", "EUR", "FIM", "FJD", "FKP", "FRF", "GBP", "GEK", "GEL", "GHC", "GHS", "GIP", "GMD", "GNF", "GNS", "GQE", "GRD", "GTQ", "GWE", "GWP", "GYD", "HKD", "HNL", "HRD", "HRK", "HTG", "HUF", "IDR", "IEP", "ILP", "ILR", "ILS", "INR", "IQD", "IRR", "ISJ", "ISK", "ITL", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRH", "KRO", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LSM", "LTL", "LTT", "LUC", "LUF", "LUL", "LVL", "LVR", "LYD", "MAD", "MAF", "MCF", "MDC", "MDL", "MGA", "MGF", "MKD", "MKN", "MLF", "MMK", "MNT", "MOP", "MRO", "MRU", "MTL", "MTP", "MUR", "MVP", "MVR", "MWK", "MXN", "MXP", "MXV", "MYR", "MZE", "MZM", "MZN", "NAD", "NGN", "NIC", "NIO", "NLG", "NOK", "NPR", "NZD", "OMR", "PAB", "PEI", "PEN", "PES", "PGK", "PHP", "PKR", "PLN", "PLZ", "PTE", "PYG", "QAR", "RHD", "ROL", "RON", "RSD", "RUB", "RUR", "RWF", "SAR", "SBD", "SCR", "SDD", "SDG", "SDP", "SEK", "SGD", "SHP", "SIT", "SKK", "SLL", "SOS", "SRD", "SRG", "SSP", "STD", "STN", "SUR", "SVC", "SYP", "SZL", "THB", "TJR", "TJS", "TMM", "TMT", "TND", "TOP", "TPE", "TRL", "TRY", "TTD", "TWD", "TZS", "UAH", "UAK", "UGS", "UGX", "USD", "USN", "USS", "UYI", "UYP", "UYU", "UZS", "VEB", "VEF", "VND", "VNN", "VUV", "WST", "XAF", "XAG", "XAU", "XBA", "XBB", "XBC", "XBD", "XCD", "XDR", "XEU", "XFO", "XFU", "XOF", "XPD", "XPF", "XPT", "XRE", "XSU", "XTS", "XUA", "XXX", "YDD", "YER", "YUD", "YUM", "YUN", "YUR", "ZAL", "ZAR", "ZMK", "ZMW", "ZRN", "ZRZ", "ZWD", "ZWL", "ZWR"]
Using the currencyCode property of the Locale class to retrieve the currency code for Sweden:
Currency Code for Sweden
new Locale("sv_SE").currencyCode
//--> "SEK"
Decimal Formatters
Decimal Formatters can be created in a variety of formatting styles:
Decimal Formatters
fmtr = Formatter.Decimal.custom
fmtr = Formatter.Decimal.decimal
fmtr = Formatter.Decimal.percent
fmtr = Formatter.Decimal.percentWithDecimal
fmtr = Formatter.Decimal.plain
fmtr = Formatter.Decimal.thousandsAndDecimal
Decimal Formatter - Decimal
dVal = Decimal.fromString(String(12345.123))
fmtr = Formatter.Decimal.decimal
fmtr.stringFromDecimal(dVal)
//--> "12345.12"
Decimal Formatter - Plain
dVal = Decimal.fromString(String(12345.123))
fmtr = Formatter.Decimal.plain
fmtr.stringFromDecimal(dVal)
//--> "12345"
Decimal Formatter - Thousands and Decimal Separator
dVal = Decimal.fromString(String(12345.123))
fmtr = Formatter.Decimal.thousandsAndDecimal
fmtr.stringFromDecimal(dVal)
//--> "12,345.12"
Decimal Formatter - Custom
dVal = Decimal.fromString(String(12345.123))
fmtr = Formatter.Decimal.custom
fmtr.decimalSeparator = "·"
fmtr.thousandsSeparator = "’"
fmtr.stringFromDecimal(dVal)
//--> "12’345·12"
Decimal Formatter - Decimal Percentage
dVal = Decimal.fromString(String(123.123))
fmtr = Formatter.Decimal.percentWithDecimal
fmtr.stringFromDecimal(dVal)
//--> "123.12%"
Decimal Formatter - Percent
dVal = Decimal.fromString(String(123.123))
fmtr = Formatter.Decimal.percent
fmtr.stringFromDecimal(dVal)
//--> "123%"
Decimal Formatter Instance Properties
Here are the properties of an instance of the Formater.Decimal class:
decimalSeparator (String) • No documentation available.
negativeFormat (String) • A format string to use for negative values.
positiveFormat (String) • A format string to use for positive values.
thousandsSeparator (String) • No documentation available.
zeroSymbol (String) • The string to use when displaying a zero value. If this is null, the positiveFormat is used.
Decimal Formatter Class Methods (functions)
Here are the methods (functions) of the Formater.Decimal class:
currency(code:String or nil) (--> Formatter.Decimal) • Returns a new formatter that will display the value as a currency value. An ISO currency code may be specified to pick a specific currency, or null may be passed to use the default currency for the user’s locale. If the argument is not a valid currency code, an error will be thrown.
Number to Swedish Kroner
code = new Locale("sv_SE").currencyCode
//--> "SEK"
fmtr = Formatter.Decimal.currency(code)
dVal = Decimal.fromString('12345')
fmtr.stringFromDecimal(dVal)
//--> SEK12,345.00
Decimal Formatter Instance Functions
Here are the methods (functions) of an instance of the Formater.Decimal class:
stringFromDecimal(number:Decimal) (--> String or nil) • Generates a corresponding string for the provided decimal value.
decimalFromString(string:String) (--> Decimal or nil) • Generates a corresponding decimal value for the provided string.
Percent Formatter
fmtr = Formatter.Decimal.percent
dVal = Decimal.fromString('79')
fmtr.stringFromDecimal(dVal)
//--> 79%
Reference Links
ISO 639-1 codes (Locale identifiers)