money2
Search…
Default format
The Currency class also allows you to specify a default format which is used when parsing or formatting a Money instance.
If you are using a Common Currency (recommended) then each has a default format pattern appropriate for that currency.
Note: If no other patterns are specified the default pattern is 'S0.00'
1
import 'package:money2/money2.dart';
2
import 'package:test/test.dart';
3
4
void main() {
5
test('Default Formatting', () {
6
final aud = Currency.create('AUD', 2, pattern: 'S0.00');
7
Money costPrice = Money.fromIntWithCurrency(1099, aud);
8
expect(costPrice.toString(), equals(r'$10.99'));
9
10
final Currency jpy = Currency.create('JPY', 0, symbol: '¥', pattern: 'S0');
11
Money yenCostPrice = Money.fromIntWithCurrency(1099, jpy);
12
expect(yenCostPrice.toString(), equals(r'¥1099'));
13
14
final euro = Currency.create('EUR', 2,
15
symbol: '€', invertSeparators: true, pattern: 'S#.##0,00');
16
Money euroCostPrice = Money.fromIntWithCurrency(899, euro);
17
expect(euroCostPrice.toString(), equals(r'€8,99'));
18
19
final usdValue = euro.parse('€7,10');
20
expect(usdValue.toString(), equals(r'€7,10'));
21
Money euroValue = euro.parse(r'€2,99');
22
expect(euroValue.toString(), equals(r'€2,99'));
23
});
24
}
25
Copied!
You can also use the Money.format method to define a specific format where required. See details below
Copy link