Default format

The Currency class also allows you to specify a default format which is used when parsing or formatting a Money instance.

If you are using a Common Currency (recommended) then each has a default format pattern appropriate for that currency.

Note: If no other patterns are specified the default pattern is 'S0.00'

import 'package:money2/money2.dart';
import 'package:test/test.dart';

void main() {
  test('Default Formatting', () {
    final aud = Currency.create('AUD', 2, pattern: 'S0.00');
    Money costPrice = Money.fromIntWithCurrency(1099, aud);
    expect(costPrice.toString(), equals(r'$10.99'));
    
    final Currency jpy = Currency.create('JPY', 0, symbol: '¥', pattern: 'S0');
    Money yenCostPrice = Money.fromIntWithCurrency(1099, jpy);
    expect(yenCostPrice.toString(), equals(r'¥1099'));

    final euro = Currency.create('EUR', 2,
        symbol: '€', invertSeparators: true, pattern: 'S#.##0,00');
    Money euroCostPrice = Money.fromIntWithCurrency(899, euro);
    expect(euroCostPrice.toString(), equals(r'€8,99'));

    final usdValue = euro.parse('€7,10');
    expect(usdValue.toString(), equals(r'€7,10'));
    Money euroValue = euro.parse(r'€2,99');
    expect(euroValue.toString(), equals(r'€2,99'));
  });
}

You can also use the Money.format method to define a specific format where required. See details below

Last updated