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 .
1
import 'package:money2/money2.dart';
2
import 'package:test/test.dart';
3
​
4
void main() {
5
test('Parse money', () {
6
/// Parse the amount using default pattern for AUD.
7
var t1 = Money.parse(r'10.00', code: 'AUD');
8
expect(t1.toString(), equals(r'$10.00'));
9
​
10
var t2 = Money.parse(r'$10.00', code: 'AUD');
11
expect(t1.amount, equals(Fixed.fromNum(10.00)));
12
​
13
/// Parse using an alternate pattern
14
var t3 = Money.parse(r'$10.00 AUD', code: 'AUD', pattern: 'S0.00 CCC');
15
expect(t1.amount, equals(Fixed.parse('10.00', scale: 2)));
16
});
17
}
18
​
Copied!

Currency.parse

Parse a monetary amount for a known currency.
1
import 'package:money2/money2.dart';
2
import 'package:test/test.dart';
3
​
4
void main() {
5
​
6
test('parse with Currency', () {
7
/// Parse using a currency.
8
var t1 = CommonCurrencies().aud.parse(r'$10.00');
9
expect(t1.toString(), equals(r'$10.00'));
10
expect(t1.currency.code, equals('AUD'));
11
});
12
}
Copied!

Currencies.parse

Allows you to parse an monetary amount using an embedded currency code to derive currency .
1
import 'package:money2/money2.dart';
2
import 'package:test/test.dart';
3
​
4
void main() {
5
6
test('parse with unknown currency', () {
7
var t1 = Currencies().parse(r'$AUD10.00');
8
expect(t1.toString(), equals(r'$10.00'));
9
expect(t1.currency.code, equals('AUD'));
10
});
11
}
Copied!

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.

​