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
  • Money.parse
  • Currency.parse
  • Currencies.parse
  • If you got this far you deserve a prize.

Was this helpful?

Parsing

The Money package provides a number of methods to parse a monetary amount:

  • Money.parse

  • Currency.parse

  • Currencies.parse

You may also find Fixed.parse is useful for parsing decimal values which can then be used to create a Money instance.

Money.parse

Allows you to parse a string containing a monetary amount.

When using Money.parse you must know the currency of the monetary amount.

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

void main() {
  test('Parse money', () {
      /// Parse the amount using default pattern for AUD.
      final t1 = Money.parse('10.00', isoCode: 'AUD');
      expect(t1.toString(), equals(r'$10.00'));

      final t2 = Money.parse(r'$10.00', isoCode: 'AUD');
      expect(t2.amount, equals(Fixed.fromNum(10.00)));

      /// Parse using an alternate pattern
      final t3 =
          Money.parse(r'$10.00 AUD', isoCode: 'AUD', pattern: 'S0.00 CCC');
      expect(t3.amount, equals(Fixed.parse('10.00', scale: 2)));
    });
}

Currency.parse

Parse a monetary amount for a known currency.

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

void main() {

  test('parse with Currency', () {
   /// Parse using a currency.
   final t1 = CommonCurrencies().aud.parse(r'$10.00');
   expect(t1.toString(), equals(r'$10.00'));
   expect(t1.currency.isoCode, equals('AUD'));
  });
}

Currencies.parse

Allows you to parse a monetary amount using an embedded currency code to derive the currency.

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

void main() {

      test('parse with unknown currency', () {
            final t1 = Currencies().parse(r'$AUD10.00');
            expect(t1.toString(), equals(r'$10.00'));
            expect(t1.currency.isoCode, equals('AUD'));
      });
}

If you got this far you deserve a prize.

If you have enough money it's good to tout.

But a word to the unwise.

Spending it gladly can lead to a drought.

PreviousRegistering a CurrencyNextFind a currency

Last updated 28 days ago

Was this helpful?