parseCodeFormat static method

CurrencyAmount parseCodeFormat(
  1. String s
)

Parses strings with format "USD 3.14".

Implementation

static CurrencyAmount parseCodeFormat(String s) {
  s = s.trim();
  final i = s.indexOf(' ');
  if (i < 0) {
    throw FormatException(
        'Expected a string with format "USD 3.14", got "$s".');
  }
  final currencyCode = s.substring(0, i);
  var currency = Currency.byCode[currencyCode];
  currency ??= Currency.specify(
    englishName: currencyCode,
    code: currencyCode,
    regionCode: null,
    fractionDigitsInPricing: 2,
  );
  final amount = Decimal(s.substring(i + 1).trim());
  return CurrencyAmount.fromDecimal(amount, currency: currency);
}