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 'S#,##0.00'

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

void main() {
  test('Default Formatting', () {
    final aud = Currency.create('AUD', 2);
      final costPrice = Money.fromIntWithCurrency(1099, aud);
      expect(costPrice.toString(), equals(r'$10.99'));

      final jpy = Currency.create('JPY', 0, symbol: '¥', pattern: 'S#,##0');
      final yenCostPrice = Money.fromIntWithCurrency(1099, jpy);
      expect(yenCostPrice.toString(), equals('¥1099'));

      final euro = Currency.create('EUR', 2,
          symbol: '€',
          decimalSeparator: ',',
          groupSeparator: '.',
          // The pattern MUST use the default group and decimal separators.
          // This allows us to share patterns across currencies.
          pattern: 'S#,##0.00');
      final euroCostPrice = Money.fromIntWithCurrency(899, euro);
      expect(euroCostPrice.toString(), equals('€8,99'));

      final euroValue = euro.parse('€2,99');
      expect(euroValue.toString(), equals('€2,99'));
  });
}

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

Last updated

Was this helpful?