Options for the formatBytes function.

interface FormatBytesOptions {
    binary?: boolean;
    bits?: boolean;
    locale?: string | boolean | readonly string[];
    maximumFractionDigits?: number;
    minimumFractionDigits?: number;
    signed?: boolean;
    space?: boolean;
}

Properties

binary?: boolean

Format the number using the Binary Prefix instead of the SI Prefix. This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes.

import formatBytes from '@oliversalzburg/js-utils/format/bytes.js';

formatBytes(1000, {binary: true});
//=> '1000 bit'

formatBytes(1024, {binary: true});
//=> '1 kiB'
bits?: boolean

Format the number as bits instead of bytes. This can be useful when, for example, referring to bit rate.

import formatBytes from '@oliversalzburg/js-utils/format/bytes.js';

formatBytes(1337, {bits: true});
//=> '1.34 kbit'
locale?: string | boolean | readonly string[]
  • If false: Output won't be localized.
  • If true: Localize the output using the system/browser locale.
  • If string: Expects a BCP 47 language tag (For example: en, de, …)
  • If string[]: Expects a list of BCP 47 language tags (For example: en, de, …)
maximumFractionDigits?: number

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.

import formatBytes from '@oliversalzburg/js-utils/format/bytes.js';

// Show the number with at most 1 fractional digit
formatBytes(1920, {maximumFractionDigits: 1});
//=> '1.9 kB'

formatBytes(1920);
//=> '1.92 kB'
minimumFractionDigits?: number

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.

import formatBytes from '@oliversalzburg/js-utils/format/bytes.js';

// Show the number with at least 3 fractional digits
formatBytes(1900, {minimumFractionDigits: 3});
//=> '1.900 kB'

formatBytes(1900);
//=> '1.9 kB'
signed?: boolean

Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment.

space?: boolean

Put a space between the number and unit.

import formatBytes from '@oliversalzburg/js-utils/format/bytes.js';

formatBytes(1920, {space: false});
//=> '1.9kB'

formatBytes(1920);
//=> '1.92 kB'