<Number>.toBytesString()

Formats a number as Bytes multiplies string.

Usage:
String = Number.toBytesString(digits = 2, locale = 'en-US', overrides = undefined)
Member of:
Number
Parameters:
  1. digits – the number of significant digits, as used by the Intl.NumberFormat function (optional, default: 2).
  2. locale – a valid BCP 47 language tag, or an Intl.Locale instance (optional, default: 'en-US').
  3. overrides – a JavaScript Object containing key-value pairs to override specific values (optional, default: undefined).
Returns:
A String representing the number of bytes, shown in an appropriate unit and locale.
Notes:
  • This function uses base-1024 IEC1) units (KiB, MiB) for output. The maximum unit magnitude supported is YiB (Yobibyte = 1024⁸)
  • The switch to the next higher unit happens at around 90% of the higher unit (see examples below).
  • This function places a narrow no-break space (U+202F) between the number and the unit to ensure they will not be separated by line breaks.

Examples

Note that JavaScript interprets a period in number literals as decimal point. To directly call this function on literal numbers, they need to be enclosed in brackets.

Example with default values:

(1010).toBytesString();  // Result: "0.99 KiB"

Example with specified digits:

(1020001).toBytesString(7);  // Result: "0.9727488 MiB"

Examples with locales:

(1010).toBytesString(2, 'de');  // Result: "0,99 KiB"
(256543000000).toBytesString(2,'ar');  // Result: "٢٤٠ MiB"

Example with override:

(0).toBytesString(2, 'en', {0: '—'});  // Result: "—"
(8).toBytesString(2, 'en', {0: '—'});  // Result: "8 Bytes"

More information