humanReadable static method

String humanReadable(
  1. num value, {
  2. int decimals = 1,
})

Formats a number in human-readable format (K, M, B)

Example:

NumberFormatters.humanReadable(1500000); // '1.5M'

Implementation

static String humanReadable(num value, {int decimals = 1}) {
  if (value < 1000) return value.toString();
  if (value < 1000000) {
    return '${(value / 1000).toStringAsFixed(decimals)}K';
  }
  if (value < 1000000000) {
    return '${(value / 1000000).toStringAsFixed(decimals)}M';
  }
  return '${(value / 1000000000).toStringAsFixed(decimals)}B';
}