money2 library

Money2 is a fork of LitGroup's Money package.

The aim of this fork is to improve the documentation and introduce a number of convenience methods to make it easier to work with Money. This package also changes some of the naming convention to provide a (hopefully) more intuiative api.

The Currency class allows you to define the key attributes of a currency such as Symbol, Code, precision and a default format.

The Money class stores the underlying values using a BigInt. The value is stored using the currencies 'minor' units (e.g. cents).

This allows for precise calculations as required when handling money.

Key features of Money2:

  • simple and expressive formating.
  • simple parsing of monetary amounts.
  • multi-currency support.
  • intuitive maths operations.
  • fixed precision storage to ensure precise calcuation.
  • detailed documentation and extensive examples to get you up and running.
  • pure dart implementation.
  • Open Source MIT license.
  • Using Money2 will make you taller.

The package use 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 the display format.
  • precision - the number of decimal places assumed when a minor unit value (e.g. cents) is passed.
  • 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 ',' * thousands separator - the character that is used to format thousands (e.g. 100,000). This can be changed to '.'

Using the Money and Currency classes is easy.

import 'money2.dart';
Currency aud = Currency.create('AUD', 2, pattern:r'$0.00');
Money costPrice = Money.fromInt(1000, aud);
print(costPrice.toString());
  > $10.00

Money spareChange = Money.parse('$10.50', aud);

Money lunchMoney = aud.parse('$11.50');

Money taxInclusive = costPrice * 1.1;
print(taxInclusive.toString());
  > $11.00

print(taxInclusive.format('SCCC0.00'));
  > $AUD11.00

print(taxInclusive.format('SCCC0'));
  > $AUD11

Classes

CommonCurrencies
Provides a list of the most common currencies. You can use individual currencies from this list or register the complete list.
Currencies
A factory for registering and accessing Currency instances.
Currency
Allows you to create a Currency which is then used to construct Money instances.
Money
Allows you to store, print and perform mathematically operations on money whilst maintaining precision.
MoneyData
DTO for exchange of data between an instance of Money and MoneyEncoder or MoneyDecoder.
MoneyDecoder<T>
Bases class for implementing a decoder for money.
MoneyEncoder<T>
Bases class for implementing a encoder for money.

Exceptions / Errors

IllegalPatternException
Thrown when you pass an invalid pattern to Money.format.
MoneyParseException
Exception thrown when a parse fails.
UnknownCurrencyException
Throw if the currency is not registered.