Money.parse constructor
Money.parse
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 isoCode
. 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.
isoCode
- the currency isoCode of the amount
. This must be either one
of the CommonCurrencies or a currency you have
registered via Currencies.register.
Throws an UnknownCurrencyException if the isoCode
is not a registered
isoCode.
Implementation
/// 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 [isoCode]. 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.
///
/// [isoCode] - the currency isoCode of the [amount]. This must be either one
/// of the [CommonCurrencies] or a currency you have
/// registered via [Currencies.register].
///
/// Throws an [UnknownCurrencyException] if the [isoCode] is not a registered
/// isoCode.
factory Money.parse(String amount,
{required String isoCode, String? pattern, int? decimalDigits}) {
if (amount.isEmpty) {
throw MoneyParseException('Empty amount passed.');
}
final currency = Currencies().find(isoCode);
if (currency == null) {
throw UnknownCurrencyException(isoCode);
}
try {
return Money.parseWithCurrency(amount, currency,
decimalDigits: decimalDigits, pattern: pattern);
// ignore: avoid_catches_without_on_clauses
} catch (e) {
throw MoneyParseException(e.toString());
}
}