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, the same in every region: '1234.500', no thousands separators, '.' as the decimal separator.

Implementation

String getFormattedQuantityString({
  String? regionString,
  bool removeZeros = false,
  bool withGroupSeparator = true,
  bool replaceComasAndAddDots = false,
}) {
  final String decimals = removeZeros ? '###' : '000';

  if (replaceComasAndAddDots) {
    return NumberFormat('0.$decimals', 'en').format(this);
  }

  final resolvedRegionString = regionString ?? DeviceLocale.dependingRegionString;

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