prettyBytes function

String prettyBytes(
  1. double number, {
  2. bool? signed,
  3. String? locale,
  4. bool? bits,
  5. bool? binary,
  6. int? minimumFractionDigits,
  7. int? maximumFractionDigits,
})

Implementation

String prettyBytes(
  double number, {
  /// Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment.
  /// Default: false.
  bool? signed,

  /// - If `null`: Output won't be localized.
  /// - If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
  /// @default null
  String? locale,

  /// Format the number as [bits](https://en.wikipedia.org/wiki/Bit) instead of [bytes](https://en.wikipedia.org/wiki/Byte). This can be useful when, for example, referring to [bit rate](https://en.wikipedia.org/wiki/Bit_rate).
  /// Default: false.
  /// @example
  /// ```
  /// prettyBytes(1337, bits: true);
  /// //=> '1.34 kbit'
  /// ```
  bool? bits,

  /// Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_prefix). This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes.
  /// Default: false.
  /// @example
  /// ```
  /// prettyBytes(1000, binary: true);
  /// //=> '1000 bit'
  /// prettyBytes(1024, binary: true);
  /// //=> '1 kiB'
  /// ```
  bool? binary,

  /// The minimum number of fraction digits to display.
  /// If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
  /// @default null
  /// ```
  /// // Show the number with at least 3 fractional digits
  /// prettyBytes(1900, {minimumFractionDigits: 3});
  /// //=> '1.900 kB'
  /// prettyBytes(1900);
  /// //=> '1.9 kB'
  /// ```
  int? minimumFractionDigits,

  /// The maximum number of fraction digits to display.
  /// If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
  /// @default null
  /// ```
  /// // Show the number with at most 1 fractional digit
  /// prettyBytes(1920, {maximumFractionDigits: 1});
  /// //=> '1.9 kB'
  /// prettyBytes(1920);
  /// //=> '1.92 kB'
  /// ```
  int? maximumFractionDigits,
}) {
  final UNITS = (bits == true)
      ? (binary == true ? BIBIT_UNITS : BIT_UNITS)
      : (binary == true ? BIBYTE_UNITS : BYTE_UNITS);

  if (signed == true && number == 0) {
    return ' 0 ${UNITS[0]}';
  }

  final isNegative = number < 0;
  final prefix = isNegative ? '-' : (signed == true ? '+' : '');

  if (isNegative) {
    number = -number;
  }

  if (number < 1) {
    final numberStr = toLocaleString(
        number, locale, minimumFractionDigits, maximumFractionDigits);
    return prefix + numberStr + ' ' + UNITS[0];
  }

  final exponent = min(
          (binary != null ? log(number) / log(1024) : log10(number) / 3)
              .floorToDouble(),
          (UNITS.length - 1).toDouble())
      .toInt();
  number /= pow(binary != null ? 1024 : 1000, exponent);
  if (minimumFractionDigits == null && maximumFractionDigits == null) {
    number = double.parse(number.toStringAsPrecision(3));
  }

  final numberStr = toLocaleString(
      number, locale, minimumFractionDigits, maximumFractionDigits);

  final unit = UNITS[exponent];

  return prefix + numberStr + ' ' + unit;
}