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?

Creating a Currency

PreviousCommon CurrenciesNextRegistering a Currency

Last updated 29 days ago

Was this helpful?

The Money2 package includes a list of most of the with the appropriate default format and scale.

In some cases you may need to create your own currency.

Creating a Currency is simple:

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

void main() {
 test('Create Currency - example 1', () {
    // US dollars which have 2 digits after the decimal place
    // using the default patttern: 'S0.00'
    final usd = Currency.create('USD', 2);
    expect(usd.isoCode, equals('USD'));

    // Create currency using a custom pattern
    final usd2 = Currency.create('USD', 2, pattern: 'SCCC 0.00');

    /// we can now use the currency to create a Money instance.
    final amount = Money.parseWithCurrency(r'$1.25', usd2);

    expect(amount.toString(), equals(r'$USD 1.25'));

    // configure everything
    final euro = Currency.create('EUR', 2,
        symbol: '€',
        decimalSeparator: ',',
        groupSeparator: '.',
        pattern: '0,00S',
        country: 'European Union',
        unit: 'Euro',
        name: 'European Union Euro');
    expect(euro.country, equals('European Union'));
  });
}

The Currency also allows you to optionally provide a country, unit and name. The common currencies have currency-specific values for each of these.

You would normally create a single instance of a Currency and re-use that throughout your code base. The easiest way to do this is to your currency.

Common Currencies
register