parse method

  1. @override
num parse(
  1. String input
)
override

Parses input and returns the numeric value.

Throws FormatException when input cannot be parsed by this codec.

Implementation

@override
num parse(String input) {
  var trimmed = input.trim();
  final isNegative = trimmed.startsWith('-');
  if (isNegative) {
    trimmed = trimmed.substring(1).trim();
  }
  late final String numberPart;

  if (symbolOnRight) {
    numberPart = stripSuffix(trimmed, symbol, require: true);
  } else if (trimmed.startsWith(symbol)) {
    numberPart = trimmed.substring(symbol.length).trim();
  } else {
    throw FormatException('Expected currency symbol "$symbol".', input);
  }

  final parsed = style.parse(numberPart);
  if (isNegative &&
      (numberPart.trim().startsWith('+') || parsed.isNegative)) {
    throw FormatException(
      'Currency value must not contain multiple signs.',
      input,
    );
  }
  return isNegative ? -parsed : parsed;
}