money2
  • Overview
  • Common Currencies
  • Creating a Currency
  • Registering a Currency
  • Parsing
  • Find a currency
  • Default format
  • Symbols
  • Separators
    • Decimal Separator
    • Group Separator
  • Creating Money
    • Money.parse
    • Currency.parse
    • Money.from
    • Currencies.parse
    • decimalDigits
  • Formatting
    • Formatting Patterns
  • Storing and Send
  • Exchange Rates
  • Comparison
  • Currency Predicates
  • Value Sign Predicates
  • Arithmetic Operations
  • Allocation
  • Money encoding/decoding
Powered by GitBook
On this page

Was this helpful?

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);
      final costPrice = Money.fromIntWithCurrency(1099, aud);
      expect(costPrice.toString(), equals(r'$10.99'));

      final jpy = Currency.create('JPY', 0, symbol: '¥', pattern: 'S0');
      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 usdValue = euro.parse('€7,10');
      expect(usdValue.toString(), equals('€7,10'));
      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

PreviousFind a currencyNextSymbols

Last updated 28 days ago

Was this helpful?