Fixed.parse constructor

Fixed.parse(
  1. String amount, {
  2. int? scale,
  3. bool invertSeparator = false,
})

Parses amount as a decimal value.

The scale controls the number of decimal places to be retained. If scale is not passed it defaults to 2.

If scale < 0 then a FixedException will be thrown. If the amount isn't valid then a FixedParseException is thrown.

If invertSeparator = false then we assume '.' is the decimal place and ',' is the group separator.

If invertSeparator = true then we assume ',' is the decimal place and '.' is the group separator.

Implementation

factory Fixed.parse(
  String amount, {
  int? scale,
  bool invertSeparator = false,
}) {
  if (scale != null) {
    _checkScale(scale);
  }

  final decimalSeparator = invertSeparator ? ',' : '.';

  final decoder = FixedDecoder(
    // ignore: flutter_style_todos
    /// TODO: remove the pattern from the decoder
    /// as I don't think we actually need one.
    /// We just need to know what char is the decimal place.
    pattern: '#$decimalSeparator#',
    groupSeparator: invertSeparator ? '.' : ',',
    decimalSeparator: invertSeparator ? ',' : '.',
  );
  final minorUnitsAndScale = decoder.decode(amount, scale);
  final targetScale = scale ?? minorUnitsAndScale.scale;
  return Fixed.fromBigInt(
      _rescale(minorUnitsAndScale.value,
          existingScale: minorUnitsAndScale.scale, targetScale: targetScale),
      scale: targetScale);
}