If you have a number and you want to format it to currency of any locale, Dart makes it easy for us to do it by using NumberFormat
class.
Currently, Dart already supports hundreds of locales, listed at the bottom of this tutorial.
There are two methods in NumbeFormat
built specifically for currency formatting: NumberFormat.currency
and NumberFormat.simpleCurrency
. The difference between those two methods is the first one uses the locale's CURRENCY_PATTERN
, while the latter uses simple symbol such as $, £, €, and Rp.
Below are the list of options for those two methods. Keep in mind that symbol
and customPattern
are not supported by .simpleCurrency
Option | Type | Description |
---|---|---|
locale |
String |
The locale to be used If not passed, it will use default locale. |
name |
String |
If passed, it will be used as the currency name. |
symbol* |
String |
If passed, the formatted number will use the symbol instead of name . |
decimalDigits |
int |
If specified, it sets how many decimal digits in the formatted output. If it's not specified and the name is defined, it will use the default for the name. Otherwise, it will use the default for the currency. |
customPattern* |
String |
It can be used to define custom pattern. |
Using NumberFormat.currency
// A1
print(NumberFormat.currency().format(123456)); // USD123,456.00
// A2
print(NumberFormat.currency(locale: 'eu').format(123456)); // 123.456,00 EUR
// A3
print(NumberFormat.currency(name: 'EURO').format(123456)); // EURO123,456.00
// A4
print(NumberFormat.currency(locale: 'eu', symbol: '?').format(123456)); // 123.456,00 ?
// A5
print(NumberFormat.currency(locale: 'eu', decimalDigits: 3).format(123456)); // 123.456,000 EUR
// A6
print(NumberFormat.currency(locale: 'eu', customPattern: '\u00a4 #,##.#').format(123456)); // EUR 12.34.56,00
In the example A1, no locale
is set and therefore it uses the systemLocale
whose default value is en_US
. It's not automatically set unless you call findSystemLocale
by importing intl_browser.dart
or intl_standalone.dart
depending on where the application runs. You can see that the result uses en_US
locale.
In the example A2, the locale
is set to eu
. You can see that the result is formatted to European from the presence of 'EUR' keyword at the end and the opposite use of .
(dot) and ,
(comma).
The third example A3 only passes name
option, which means the default locale
is used. It only replaces 'USD' with 'EURO'.
The example A4 uses eu
as the locale
and ?
as the symbol
replacing name
.
The example A5 set decimalDigits
to specify the number of decimal digits.
The example A6 uses customPattern
. The \u00a4
part is the unicode character of currency sign, while the pattern #,##.#
means every two digits will be separated by separator (instead of every three digits).
*
: Not supported by .simpleCurrency
Using NumberFormat.simpleCurrency
Using this method, by default you will get the symbol instead of the name of the currency. You can see the symbol mapping at bottom of this example.
# B1
print(NumberFormat.simpleCurrency().format(123456)); // $123,456.00
# B2
print(NumberFormat.simpleCurrency(locale: 'eu').format(123456)); // 123.456,00 €
# B3
print(NumberFormat.simpleCurrency(name: 'EURO').format(123456)); // EURO123,456.00
# B4
print(NumberFormat.simpleCurrency(locale: 'eu', decimalDigits: 3).format(123456)); // 123.456,000 €
The example B1 doesn't set the locale
, so it will use the default locale as explained on .currency
method section. From the result, you can see that it uses the $
symbol instead of the currency name.
In the example B2, locale
is set to eu
. You'll get the European formatted currency with € symbol.
In the example B3, by passing name
, it replaces the usage of symbol.
The last example B4 sets number of decimal digits in the output.
List of Supported Locales
Here's the list of supported locales. If you try to use non-supported locale, you will get Invalid argument exception.
af
: ZARam
: ETBar
: EGPar_DZ
: DZDar_EG
: EGPaz
: AZNbe
: BYNbg
: BGNbn
: BDTbr
: EURbs
: BAMca
: EURchr
: USDcs
: CZKcy
: GBPda
: DKKde
: EURde_AT
: EURde_CH
: CHFel
: EURen
: USDen_AU
: AUDen_CA
: CADen_GB
: GBPen_IE
: EURen_IN
: INRen_MY
: MYRen_SG
: SGDen_US
: USDen_ZA
: ZARes
: EURes_419
: MXNes_ES
: EURes_MX
: MXNes_US
: USDet
: EUReu
: EURfa
: IRRfi
: EURfil
: PHPfr
: EURfr_CA
: CADfr_CH
: CHFga
: EURgl
: EURgsw
: CHFgu
: INRhaw
: USDhe
: ILShi
: INRhr
: HRKhu
: HUFhy
: AMDid
: IDRin
: IDRis
: ISKit
: EURit_CH
: CHFiw
: ILSja
: JPYka
: GELkk
: KZTkm
: KHRkn
: INRko
: KRWky
: KGSln
: CDFlo
: LAKlt
: EURlv
: EURmk
: MKDml
: INRmn
: MNTmr
: INRms
: MYRmt
: EURmy
: MMKnb
: NOKne
: NPRnl
: EURno
: NOKno_NO
: NOKor
: INRpa
: INRpl
: PLNps
: AFNpt
: BRLpt_BR
: BRLpt_PT
: EURro
: RONru
: RUBsi
: LKRsk
: EURsl
: EURsq
: ALLsr
: RSDsr_Latn
: RSDsv
: SEKsw
: TZSta
: INRte
: INRth
: THBtl
: PHPtr
: TRYuk
: UAHur
: PKRuz
: UZSvi
: VNDzh
: CNYzh_CN
: CNYzh_HK
: HKDzh_TW
: TWDzu
: ZAR
Mapping to Simple Currencies
- AFN: Af.
- TOP": r"T$
- MGA: Ar
- THB: \u0e3f
- PAB: B/.
- ETB: Birr
- VEF: Bs
- BOB: Bs
- GHS: GHS
- CRC: \u20a1
- NIO": r"C$
- GMD: GMD
- MKD: din
- BHD: din
- DZD: din
- IQD: din
- JOD: din
- KWD: din
- LYD: din
- RSD: din
- TND: din
- AED: dh
- MAD: dh
- STD: Db
- BSD": r"$
- FJD": r"$
- GYD": r"$
- KYD": r"$
- LRD": r"$
- SBD": r"$
- SRD": r"$
- AUD": r"$
- BBD": r"$
- BMD": r"$
- BND": r"$
- BZD": r"$
- CAD": r"$
- HKD": r"$
- JMD": r"$
- NAD": r"$
- NZD": r"$
- SGD": r"$
- TTD": r"$
- TWD": r"NT$
- USD": r"$
- XCD": r"$
- VND: \u20ab
- AMD: Dram
- CVE: CVE
- EUR: \u20ac
- AWG: Afl.
- HUF: Ft
- BIF: FBu
- CDF: FrCD
- CHF: CHF
- DJF: Fdj
- GNF: FG
- RWF: RF
- XOF: CFA
- XPF: FCFP
- KMF: CF
- XAF: FCFA
- HTG: HTG
- PYG: Gs
- UAH: \u20b4
- PGK: PGK
- LAK: \u20ad
- CZK: K\u010d
- SEK: kr
- ISK: kr
- DKK: kr
- NOK: kr
- HRK: kn
- MWK: MWK
- ZMK: ZWK
- AOA: Kz
- MMK: K
- GEL: GEL
- LVL: Ls
- ALL: Lek
- HNL: L
- SLL: SLL
- MDL: MDL
- RON: RON
- BGN: lev
- SZL: SZL
- TRY: TL
- LTL: Lt
- LSL: LSL
- AZN: man.
- BAM: KM
- MZN: MTn
- NGN: \u20a6
- ERN: Nfk
- BTN: Nu.
- MRO: MRO
- MOP: MOP
- CUP": r"$
- CUC": r"$
- ARS": r"$
- CLF: UF
- CLP": r"$
- COP": r"$
- DOP": r"$
- MXN": r"$
- PHP: \u20b1
- UYU": r"$
- FKP: £
- GIP: £
- SHP: £
- EGP: E£
- LBP: L£
- SDG: SDG
- SSP: SSP
- GBP: £
- SYP: £
- BWP: P
- GTQ: Q
- ZAR: R
- BRL": r"R$
- OMR: Rial
- QAR: Rial
- YER: Rial
- IRR: Rial
- KHR: Riel
- MYR: RM
- SAR: Riyal
- BYR: BYR
- RUB: руб.
- MUR: Rs
- SCR: SCR
- LKR: Rs
- NPR: Rs
- INR: \u20b9
- PKR: Rs
- IDR: Rp
- ILS: \u20aa
- KES: Ksh
- SOS: SOS
- TZS: TSh
- UGX: UGX
- PEN: S/.
- KGS: KGS
- UZS: so\u02bcm
- TJS: Som
- BDT: \u09f3
- WST: WST
- KZT: \u20b8
- MNT: \u20ae
- VUV: VUV
- KPW: \u20a9
- KRW: \u20a9
- JPY: ¥
- CNY: ¥
- PLN: z\u0142
- MVR: Rf
- NLG: NAf
- ZMW: ZK
- ANG: ƒ
- TMT: TMT