getFormattedQuantityString method

String getFormattedQuantityString({
  1. String? regionString,
  2. bool removeZeros = false,
  3. bool withGroupSeparator = true,
  4. bool replaceComasAndAddDots = false,
})

Formats the quantity for the region: 3 decimals, or up to 3 with removeZeros.

replaceComasAndAddDots gives the API format instead: '1234.500', no thousands separators, '.' as the decimal separator, whatever the region and the device settings are.

Implementation

String getFormattedQuantityString({
  String? regionString,
  bool removeZeros = false,
  bool withGroupSeparator = true,
  bool replaceComasAndAddDots = false,
}) {
  if (replaceComasAndAddDots) {
    // Not NumberFormat: apps patch the separators of every locale to the device ones
    final String fixed = toStringAsFixed(3);
    return removeZeros ? fixed.replaceFirst(RegExp(r'\.?0+$'), '') : fixed;
  }

  final resolvedRegionString = regionString ?? DeviceLocale.dependingRegionString;

  return NumberFormat(
    '#${withGroupSeparator ? ',' : ''}##0.${removeZeros ? '###' : '000'}',
    resolvedRegionString,
  ).format(this).replaceAll(RegExp(r'[  ’]'), '');
}