money2
Search…
Creating a Currency
The Money2 package includes a list of most of the common currencies with the appropriate default format and scale.
In some cases you may need to create your own currency.
The Money2 package does contain a list of Common Currencies and you can also create your own.
Creating a Currency is simple:
1
import 'package:money2/money2.dart';
2
import 'package:test/test.dart';
3
4
void main() {
5
test('Create Currency - example 1', () {
6
// US dollars which have 2 digits after the decimal place
7
// using the default patttern: 'S0.00'
8
final usd = Currency.create('USD', 2);
9
10
// Create currency using a custom pattern
11
final usd2 = Currency.create('USD', 2, pattern: r'SCCC 0.00');
12
13
/// we can now use the currency to create a Money instance.
14
final amount = Money.parseWithCurrency(r'$1.25', usd2);
15
16
expect(amount.toString(), equals(r'$USD 1.25'));
17
18
// configure everything
19
final Currency euro = Currency.create('EUR', 2,
20
symbol: '€',
21
invertSeparators: true,
22
pattern: '0,00S',
23
country: 'European Union',
24
unit: 'Euro',
25
name: 'European Union Euro');
26
});
27
}
Copied!
The Currency also allows you to optionally provide a country, unit and name. The common currencies have currency specific values for each of these.
You would normally create a single instance of a Currency and re-use that throughout your code base. The easiest way to do this is to register your currency.
Copy link