money2
Search…
Overview
Money2 is a Dart package providing parsing, formatting and mathematical operations on monetary amounts.
Money2 is a Dart package providing parsing, formatting and mathematical operations on monetary amounts.
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 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.

Sponsored by OnePub

Help support Money2 by supporting OnePub, the private Dart repository.
OnePub allows you to privately share Dart packages between your own projects or with colleagues.
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 private
dart pub publish
Full API Documentation can be found at:
money2 - Dart API docs
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:
1
import 'package:money2/money2.dart';
2
import 'package:test/test.dart';
3
4
void main() {
5
test('Overview - example 1', () {
6
Currency usdCurrency = Currency.create('USD', 2);
7
// Create money from an int.
8
Money costPrice = Money.fromIntWithCurrency(1000, usdCurrency);
9
expect(costPrice.toString(), equals(r'$10.00'));
10
11
final taxInclusive = costPrice * 1.1;
12
expect(taxInclusive.toString(), equals(r'$11.00'));
13
14
expect(taxInclusive.format('SCC #.00'), equals(r'$US 11.00'));
15
16
// Create money from an String using the `Currency` instance.
17
Money parsed = usdCurrency.parse(r'$10.00');
18
expect(parsed.format('SCCC 0.00'), equals(r'$USD 10.00'));
19
20
// Create money from an int which contains the MajorUnit (e.g dollars)
21
Money buyPrice = Money.fromNum(10, code: 'USD');
22
expect(buyPrice.toString(), r'$10.00');
23
24
// Create money from a double which contains Major and Minor units (e.g. dollars and cents)
25
// We don't recommend transporting money as a double as you will get rounding errors.
26
Money sellPrice = Money.fromNum(10.50, code: 'AUD');
27
expect(sellPrice.toString(), equals(r'$10.50'));
28
});
29
}
30
Copied!
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. '#x27;. 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 '.'
Last modified 10d ago
Copy link