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'));
    });

Last updated

Was this helpful?