money2
Search
⌃K

Parsing

The Money package provides a number methods to parse a monetary amount:
  • Money.parse
  • Currency.parse
  • Currencies.parse
You may also find Fixed.parse useful to 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 what currency of the amount .
import 'package:money2/money2.dart';
import 'package:test/test.dart';
void main() {
test('Parse money', () {
/// Parse the amount using default pattern for AUD.
var t1 = Money.parse(r'10.00', code: 'AUD');
expect(t1.toString(), equals(r'$10.00'));
var t2 = Money.parse(r'$10.00', code: 'AUD');
expect(t1.amount, equals(Fixed.fromNum(10.00)));
/// Parse using an alternate pattern
var t3 = Money.parse(r'$10.00 AUD', code: 'AUD', pattern: 'S0.00 CCC');
expect(t1.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.
var t1 = CommonCurrencies().aud.parse(r'$10.00');
expect(t1.toString(), equals(r'$10.00'));
expect(t1.currency.code, equals('AUD'));
});
}

Currencies.parse

Allows you to parse an monetary amount using an embedded currency code to derive currency .
import 'package:money2/money2.dart';
import 'package:test/test.dart';
void main() {
test('parse with unknown currency', () {
var t1 = Currencies().parse(r'$AUD10.00');
expect(t1.toString(), equals(r'$10.00'));
expect(t1.currency.code, equals('AUD'));
});
}

If you got this far you deserve a prize.

If you have enough money its good to tout.
But a word to the unwise.
Spending it gladly can lead to a drought.