money2 library Null safety
Money2 stores, parses, formats and allows precision mathematical operations on monetary amounts and their associated currency.
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.
The ExchangePlatform allows you to set up a table of exchange rates for converting between currencies.
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 ','
- group separator - the character that is used to format groups (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
- Provides a list of the most common currencies.
- A factory for registering, parsing and finding Currency instances.
- Allows you to create a Currency which is then used to construct Money instances.
- The ExchangePlatform allows you to register a set of ExchangeRate which you can then use to do currency conversions.
- When defining an exchange rate we need to specify the conditions under which the exchange is calculated.
- Represents a fixed scale decimal no.
- Allows you to store, print and perform mathematically operations on money whilst maintaining precision.
DTO for exchange of data between an instance of Money and
- Bases class for implementing a decoder for money.
- Bases class for implementing a encoder for money.
Exceptions / Errors
- Thrown if a number larger than the supported ranges is passed in. This will only occur if you call Fixed.fromNum with scale > 20 or a absolute value of > 10^21 If you need larger numbers then use one of the alternate constructors.
- Base exception of all exceptions thrown from the Fixed package.
- Thrown when you pass an invalid pattern to Money.format.
- Thrown if an exchange is attempted with a Money has a Currency which doesn't match the exchange rate.
- Base class of all exceptions thrown from Money2.
- Exception thrown when a parse fails.
- Thrown if the currency is not registered.
- Thrown if an attempt is made to calcuate the value of a Money amount in another currency for which there isn't a registered exchange rate.