toHumanReadable method

String toHumanReadable({
  1. int decimals = 1,
})

Converts the number to a human-readable string (e.g., 1K, 1M).

Example:

1000.toHumanReadable(); // '1K'
1500000.toHumanReadable(); // '1.5M'

Implementation

String toHumanReadable({int decimals = 1}) {
  if (this < 1000) return toString();
  if (this < 1000000) {
    return '${(this / 1000).toStringAsFixed(decimals)}K';
  }
  if (this < 1000000000) {
    return '${(this / 1000000).toStringAsFixed(decimals)}M';
  }
  return '${(this / 1000000000).toStringAsFixed(decimals)}B';
}