parseDoubleIntoAmount method

String parseDoubleIntoAmount({
  1. int decimals = 2,
  2. String decimalSeparator = ",",
  3. String thousandsSeparator = " ",
})

Formats the double into a human-readable amount with decimals decimals, using a comma (,) as decimal separator and spaces ( ) as thousands separators.

Example:

12345.678.parseDoubleIntoAmount(); // "12 345,68"
12345.6.parseDoubleIntoAmount(decimals: 0); // "12 346"

Implementation

String parseDoubleIntoAmount({ int decimals = 2, String decimalSeparator = ",", String thousandsSeparator = " " }) {
  String parsed = toStringAsFixed(decimals).replaceAll('.', decimalSeparator);

  final result = parsed.replaceAllMapped(
      RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'), (Match m) => '${m[1]} ');
  return result.replaceAll(" ", thousandsSeparator);
}