money2
  • Overview
  • Common Currencies
  • Creating a Currency
  • Registering a Currency
  • Parsing
  • Find a currency
  • Default format
  • Symbols
  • Separators
    • Decimal Separator
    • Group Separator
  • Creating Money
    • Money.parse
    • Currency.parse
    • Money.from
    • Currencies.parse
    • decimalDigits
  • Formatting
    • Formatting Patterns
  • Storing and Send
  • Exchange Rates
  • Comparison
  • Currency Predicates
  • Value Sign Predicates
  • Arithmetic Operations
  • Allocation
  • Money encoding/decoding
Powered by GitBook
On this page

Was this helpful?

Overview

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

NextCommon Currencies

Last updated 28 days ago

Was this helpful?

Key features of Money2:

  • simple and expressive formatting.

  • simple parsing of monetary amounts.

  • multi-currency support.

  • intuitive maths operations.

  • fixed precision storage to ensure calculation without loss of precision.

  • 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 , 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:

Let's start with some examples:

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

void main() {
  test('Overview - example 1', () {
   final usdCurrency = Currency.create('USD', 2);

    /// Create money from an int.
    final 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.
    final 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)
    final buyPrice = Money.fromNum(10, isoCode: 'AUD');
    expect(buyPrice.toString(), equals(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.
    final sellPrice = Money.fromNum(10.50, isoCode: '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

  • isoCode - 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.

  • decimals - 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 any character.

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

Note: Money2 is tested to a maximum of 100 integer digits and 100 decimal digits. Money2 is likely to work with larger numbers as under the hood we use a BigInt which in Dart is only limited by memory.

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

OnePub
Fixed
https://github.com/onepub-dev/money.dart/issues/75
money2 - Dart API docs
Logo