NPM module for converting and formatting data sizes.
npm install format-data-size
import { formatDataSize, formatDataSizeToString } from 'format-data-size';
formatDataSize(1298);
// => { value: '1.30', unit: 'kB' }
formatDataSizeToString(11223.3, { fromUnit: 'kB' });
// => '11.22 MB'
Specifies the value to be converted and/or formatted.
Contains the options that can change the way in which input are read and output are manipulated.
Specifies the data unit of the provided value.
Specifies the locale of the output. This only affects the numbers, grouping characters, and decimal character. true
means to use system locale, while a [BCP 47 language tag]
, i.e., 'de'
, specifies the target language.
Specifies the result's precision. [number]
means to return the value with exactly the given number of fraction digits, while an object containing properties min
and/or max
results in the return value possessing the number of fraction digits between min
and max
(inclusive).
Specifies which unit or type of units to format the input to. When not provided, the most reasonable target unit is auto selected based on the source unit, i.e.:
- A 4 digit integral kilobyte (
kB
) will become a 1 digit megabyte (MB
). - A 6 digit integral megabit (
Mbit
) will become a 3 digit gigabit (Gbit
).
It is possible to provide one of 'byte'
, 'ibyte'
(binary), 'bit'
, or 'ibit'
(binary) to reduce the scope of the auto selection.
The primary goal of this package is to support working with the bigint
primitive type. Many packages with the same purpose lack such support. As memory and storage mediums continue to increase in size, we need larger numbers to represent them. However, working with bigint
introduces new costs because there are limited built-in functionalities that can handle it, i.e., the Math
functions and some operators such as x ** y
are unavailable. Therefore, the initial versions of this package are expected to be less efficient.