getFormattedQuantityString method

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

Implementation

String getFormattedQuantityString({
  String? regionString,
  bool removeZeros = false,
  bool withGroupSeparator = true,
  bool replaceComasAndAddDots = false,
}) {
  final resolvedRegionString = regionString ?? DeviceLocale.dependingRegionString;

  final String formattedString = NumberFormat(
    '#${withGroupSeparator ? ',' : ''}##0.${removeZeros ? '###' : '000'}',
    resolvedRegionString,
  ).format(this).replaceAll(RegExp(r'[\u00A0\u202f\u2019]'), '');

  if (replaceComasAndAddDots) {
    final String dotsString = formattedString.replaceAll(',', '.');

    if (!dotsString.contains('.')) {
      return '$formattedString.000';
    } else {
      final int lastDotIndex = dotsString.lastIndexOf('.');

      final String leftPart = dotsString
          .substring(0, lastDotIndex)
          .replaceAll('.', '');
      final String rightPart = dotsString.substring(
        lastDotIndex,
        dotsString.length,
      );

      return '$leftPart$rightPart';
    }
  }

  return formattedString;
}