== .toBytesString ==

.toBytesString()

Formats a number as Bytes multiplies string.
Usage:
[[mdn>Web/JavaScript/Reference/Global_Objects/String|String]] = Number''.toBytesString''(digits = ''2'', locale = '''en-US''', overrides = ''undefined'')
Member of:
[[mdn>Web/JavaScript/Reference/Global_Objects/Number|Number]]
Parameters:
- digits – the number of significant digits, as used by the [[mdn>Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat|Intl.NumberFormat]] function (optional, default: ''2''). - locale – a valid [[wp>IETF language tag|BCP 47 language tag]], or an [[mdn>Web/JavaScript/Reference/Global_Objects/Intl/Locale|Intl.Locale]] instance (optional, default: '''en-US'''). - overrides – a JavaScript [[mdn>Web/JavaScript/Reference/Global_Objects/Object|Object]] containing key-value pairs to override specific values (optional, default: ''undefined'').
Returns:
A [[mdn>Web/JavaScript/Reference/Global_Objects/String|String]] representing the number of bytes, shown in an appropriate unit and locale.
Notes:
* This function uses base-1024 IEC((See: [[https://www.iso.org/standard/87648.html|IEC 80000-13:2025]])) 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 ([[https://unicodeplus.com/U+202F|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 ===== * [[mdn>Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat|Intl.NumberFormat]] on MDN.