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
  • Common Currencies
  • Register a Currency

Was this helpful?

Registering a Currency

PreviousCreating a CurrencyNextParsing

Last updated 21 days ago

Was this helpful?

Money2 ships with a list of however there are times when you may want to add additional currencies (e.g. digital currencies) or change the defaults for an existing Common Currency.

Common Currencies

Common Currencies are pre-loaded into the currency Registry in Money2. You can not modify a Common Currency but you can replace it's entry in the Money2 registry.

This means that when parsing a string containing an isoCode or using Currencies().find the modified Currency will be returned.

Register a Currency

When creating custom Currencies you have two choices, register the currency , with the benefit that you can access it like any other currency, or create and manage the currency in your own code.

Both methods are useful at different times so use the one that best suits your use case.

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

void main() {
 test('register currency', () {
      /// Create currency and replace the CommonCurrency with our
      /// own one.
      final usd = Currency.create('USD', 2);
      Currencies().register(usd);

      /// Change the registered euro currency to have 4 decimal places
      /// Note: CommonCurrencies can't be changed but the registry can.
      final euro = CommonCurrencies().euro.copyWith(decimalDigits: 4);
      Currencies().register(euro);
      final euro4 = Currencies().parse('EUR1500.0');
      expect(euro4.decimalDigits, equals(4));

      /// register a new currency with 8 decimals.
      final doge =
          Currency.create('DODG', 8, symbol: 'Ð', pattern: 'S0.00000000');
      Currencies().register(doge);

      // find a registered currency.
      final nowUseIt = Currencies().find('DODG');
      expect(nowUseIt, isNotNull);
      if (nowUseIt != null) {
        final cost = Money.fromIntWithCurrency(1000000000, nowUseIt);
        expect(cost.toString(), equals('Ð10.00000000'));
      }
    });
}
Common Currencies