prettyFormat property

String get prettyFormat

Converts numbers to human-readable strings.

This function takes a numeric input and formats it into a more concise, human-readable string representation, using abbreviations for large numbers.

Examples:

  • 3300 -> "3.3k"
  • 2300000 -> "2.3M"
  • 1200000000 -> "1.2B"

Behavior:

  • Numbers less than 1000 are returned as their string representation.
  • Numbers between 1000 and 1,000,000 are divided by 1000 and appended with "k" (thousands).
  • Numbers between 1,000,000 and 1,000,000,000 are divided by 1,000,000 and appended with "M" (millions).
  • Numbers greater than or equal to 1,000,000,000 are divided by 1,000,000,000 and appended with "B" (billions).
  • The result is formatted to one decimal place.

Parameters:

  • number: The numeric value to be converted.

Returns:

A human-readable string representation of the input number.

Implementation

String get prettyFormat {
  if (this < 1000) {
    return toString();
  } else if (this < 1_000_000) {
    return "${(this / 1000.0).toStringAsFixed(1)}k";
  } else if (this < 1_000_000_000) {
    return "${(this / 1_000_000.0).toStringAsFixed(1)}M";
  } else {
    return "${(this / 1_000_000_000.0).toStringAsFixed(1)}B";
  }
}