Money.parseWithCurrency constructor
Money.parseWithCurrency(})
Parses the passed amount
and returns a Money instance.
The passed amount
must match the given pattern
or
if no pattern is supplied the the default pattern of the
currency indicated by the currency
. The S and C characters
in the pattern are optional.
See format for details on how to construct a pattern.
Throws an MoneyParseException if the amount
doesn't
match the pattern.
If the number of decimals in amount
exceeds the Currency's
decimalDigits then excess digits will be ignored.
currency
- the currency of the amount
.
Implementation
factory Money.parseWithCurrency(String amount, Currency currency,
{String? pattern, int? decimalDigits}) {
if (amount.isEmpty) {
throw MoneyParseException('Empty amount passed.');
}
try {
pattern ??= currency.pattern;
final decoder = PatternDecoder(currency, pattern);
final data = decoder.decode(amount);
return Money._from(
Fixed.copyWith(data.amount,
scale: decimalDigits ?? currency.decimalDigits),
currency);
// ignore: avoid_catches_without_on_clauses
} catch (e) {
throw MoneyParseException(e.toString());
}
}