toReadable method
Formats this double as a readable number with K, M, B, T suffixes. Automatically chooses the appropriate suffix based on value.
Example:
double value = 1500.0;
String readable = value.toReadable(); // '1.5K'
1500000.0.toReadable(); // '1.5M'
1500000000.0.toReadable(); // '1.5B'
1500000000000.0.toReadable(); // '1.5T'
Implementation
String toReadable() {
final absValue = abs();
final isNegative = this < 0;
final sign = isNegative ? '-' : '';
if (absValue < 1000) {
return '$sign${toStringAsFixed(absValue % 1 == 0 ? 0 : 2)}';
}
if (absValue < 1000000) return '$sign${(absValue / 1000).toStringAsFixed(1)}K';
if (absValue < 1000000000) return '$sign${(absValue / 1000000).toStringAsFixed(1)}M';
if (absValue < 1000000000000) return '$sign${(absValue / 1000000000).toStringAsFixed(1)}B';
return '$sign${(absValue / 1000000000000).toStringAsFixed(1)}T';
}