parse method

Money parse(
  1. String monetaryAmount, {
  2. String? pattern,
})

Takes a monetary amount encoded as a string and converts it to a Money instance.

You can pass in a pattern to define the format of the monetaryAmount. If you don't pass in a pattern then the Currencys default pattern is used.

If the number of minorUnits in monetaryAmount exceeds the Currencys precision then excess digits will be ignored.

Currency aud = Currency.create('AUD', 2); Money audAmount = aud.parse('10.50');

A MoneyParseException is thrown if the monetaryAmount doesn't match the pattern.

Implementation

Money parse(String monetaryAmount, {String? pattern}) {
  if (monetaryAmount.isEmpty) {
    throw MoneyParseException('Empty monetaryAmount passed.');
  }
  pattern ??= this.pattern;
  final decoder = PatternDecoder(this, pattern);
  final moneyData = decoder.decode(monetaryAmount);

  return Money.fromFixedWithCurrency(moneyData.amount, this);
}