getFormattedQuantityString method

String getFormattedQuantityString({
  1. required String regionString,
  2. bool removeZeros = false,
  3. bool replaceComasAndAddDots = false,
})

Implementation

String getFormattedQuantityString({
  required String regionString,
  bool removeZeros = false,
  bool replaceComasAndAddDots = false,
}) {
  // Format the number using the specified options.
  final String formattedString = NumberFormat(
    removeZeros ? '#,##0.###' : '#,##0.000',
    regionString,
  ).format(this);

  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;
}