Money2 is a Dart package providing parsing, formatting and mathematical operations on monetary amounts.

Key features of Money2:

  • simple and expressive formatting.

  • simple parsing of monetary amounts.

  • multi-currency support.

  • intuitive maths operations.

  • fixed precision storage to ensure precise calculation.

  • detailed documentation and extensive examples to get you up and running.

  • pure Dart implementation.

  • Open Source MIT license.

  • Using Money2 will make you taller.

Help support Money2 by supporting OnePub, the private Dart repository.

OnePub allows you to privately share Dart packages across your Team and with your customers.

Try it for free and publish your first private package in seconds.

Publish a private package in five commands:

dart pub global activate onepub

onepub login cd <my package> onepub pub private dart pub publish

Full API Documentation can be found at:

Essentially the Money class stores the monetary value as a fixed scale (number of decimal places) decimal using the Fixed package. This allows for precise calculations as required when handling money and eliminates common rounding issues.

Let's start with some examples:

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

void main() {
  test('Overview - example 1', () {
    Currency usdCurrency = Currency.create('USD', 2);
    // Create money from an int.
    Money costPrice = Money.fromIntWithCurrency(1000, usdCurrency);
    expect(costPrice.toString(), equals(r'$10.00'));

    final taxInclusive = costPrice * 1.1;
    expect(taxInclusive.toString(), equals(r'$11.00'));

    expect(taxInclusive.format('SCC #.00'), equals(r'$US 11.00'));

    // Create money from an String using the `Currency` instance.
    Money parsed = usdCurrency.parse(r'$10.00');
    expect(parsed.format('SCCC 0.00'), equals(r'$USD 10.00'));

    // Create money from an int which contains the MajorUnit (e.g dollars)
    Money buyPrice = Money.fromNum(10, code: 'USD');
    expect(buyPrice.toString(), r'$10.00');

    // Create money from a double which contains Major and Minor units (e.g. dollars and cents)
    // We don't recommend transporting money as a double as you will get rounding errors.
    Money sellPrice = Money.fromNum(10.50, code: 'AUD');
    expect(sellPrice.toString(), equals(r'$10.50'));

The package uses the following terms:

  • Minor Units - the smallest unit of a currency e.g. cents.

  • Major Units - the integer component of a currency - e.g. dollars

  • code - the currency code. e.g. USD

  • symbol - the currency symbol. e.g. '$'. It should be noted that not every currency has a symbol.

  • pattern - a pattern used to control parsing and the display format.

  • scale - the number of minor Units (e.g. cents) which should be used when storing the currency.

  • decimal separator - the character that separates the fraction part from the integer of a number e.g. '10.99'. This defaults to '.' but can be changed to ','

  • group separator - the character that is used to format thousands (e.g. 100,000). Defaults to ',' but can be changed to '.'

Note: Money2 has a maximum scale (decimals) and integer size of 18 digits. This is a somewhat arbitrary limit so raise a PR to fix the issue the help would be appreciated.


Last updated