Formatting Patterns

Note: the same patterns are used for both formatting and parsing monetary amounts.

The supported pattern characters are:

  • S outputs the currencies symbol e.g. $.

  • C outputs part of the currency code e.g. USD. You can specify 1,2 or 3 C's. Specifying CCC will output the full code regardless of its length.

    • C - U

    • CC - US

    • CCC - USD - outputs the full currency code regardless of length.

  • denotes a digit.

  • 0 denotes a digit and and forces padding with leading and trailing zeros.

  • , (comma) a placeholder for the grouping separtor

  • . (period) a place holder for the decimal separator

Examples:

import 'money2.dart';
final Currency usd = Currency.create('USD', 2);
Money lowPrice = Money.fromInt(1099, usd);
print(lowPrice.format('S000.000'));
  > $010.990

Money costPrice = Money.fromInt(10034530, usd);  // 100,345.30 usd

print(costPrice.format('###,###.##'));
  > 100,345.30

print(costPrice.format('S###,###.##'));
  > $100,345.3

print(costPrice.format('CC###,###.#0'));
  > US100,345.30

print(costPrice.format('CCC###,###.##'));
  > USD100,345.3

print(costPrice.format('SCC###,###.#0'));
  > $US100,345.30

final usd = Currency.create('USD', 2);
Money costPrice = Money.fromInt(10034530, usd);  // 100,345.30 usd
print(costPrice.format('SCC###,###.##'));
  > $US100,345.3

final jpy = Currency.create('JPY', 0, symbol: '¥');
Money costPrice = Money.fromInt(345, jpy);  // 345 yen
print(costPrice.format('SCCC#'));
  > ¥JPY345

// Bahraini dinar
final bhd = Currency.create('BHD', 3, symbol: 'BD', invertSeparators: true);
Money costPrice = Money.withInt(100345, bhd);  // 100.345 bhd
print(costPrice.format('SCCC0000,###')); 
  > BDBHD0100,345

Last updated