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?

Storing and Send

When storing and sending monetary amounts you need to ensure that you retain the precision and decimal digits of the amount.

The best way to do this is to store the amount is three components.

  • Minor units

  • Decimal Digits

  • The currencies ISO Code

A value of $AUD1.99 would be stored as:

Minor Units: 199

Decimal Digits: 2

ISO Code: AUD

Using this technique guarantees you will always get back the amount you stored or transmitted.

If you are only using a single currency the Fixed package provides tools to store the minor units and decimal digits.

The Money package allows you to convert a Money amount to from a json format.


     test('formatting', () {
      final money = Money.fromInt(1025, isoCode: 'USD');
      final moneyJson = money.toJson();

      final expectedJson = <String, dynamic>{
        'minorUnits': '1025',
        'decimals': 2,
        'isoCode': 'USD',
      };

      expect(moneyJson, equals(expectedJson));

      final retrievedAmount = Money.fromJson(moneyJson);
      expect(retrievedAmount.minorUnits.toInt(), equals(1025));
      expect(retrievedAmount.decimalDigits, equals(2));
      expect(retrievedAmount.currency.isoCode, equals('USD'));
    });
PreviousFormatting PatternsNextExchange Rates

Last updated 28 days ago

Was this helpful?