Superbytes is a Node.js library for converting bytes to human readable string format
Superbytes will help you convert bytes into other units understandable for humans. The library automatically converts bytes and returns a string with the most optimal unit representation. The library also allows you to set the precision of numbers following the decimal point and choose the output metric in the form of either the IEC standard (1024 bytes = 1 kibibyte) or SI (1000 bytes = 1 kilobyte).
By default superbytes converts to IEC units.
The current version supports loading library using both CommonJS and ESModules. The library is written in TypeScript, ships with type definitions and has zero dependencies.
Latest version:
npm i superbytes@latestconst { superbytes } = require('superbytes');
superbytes(423551030);
// returns '403.93 MiB'
superbytes(423551030, 3);
// returns '403.930 MiB'
superbytes(72355103011, { metric: 'si'});
// returns '72.36 GB'
superbytes(3123123, { precision: 5});
// returns '2.97844 MiB'
superbytes(912839123, { metric: 'si', precision: 5});
// returns '912.83912 MB'import { superbytes } from 'superbytes';
superbytes(423551030);
// returns '403.93 MiB'
superbytes(423551030, 3);
// returns '403.930 MiB'
superbytes(72355103011, { metric: 'si'});
// returns '72.36 GB'
superbytes(3123123, { precision: 5});
// returns '2.97844 MiB'
superbytes(912839123, { metric: 'si', precision: 5});
// returns '912.83912 MB'| Metric | Divider | Units |
|---|---|---|
| IEC (default) | 1024 | B, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB |
| SI | 1000 | B, kB, MB, GB, TB, PB, EB, ZB, YB |
- Negative values are converted as their absolute value, e.g.
superbytes(-423551030)returns'403.93 MiB'. - Values smaller than the divider are returned without decimal formatting, e.g.
superbytes(1023)returns'1023 B'. - A
TypeErroris thrown for non-finite values (Infinity,NaN). - A
RangeErroris thrown when the value exceeds the largest supported unit (YiB/YB).
MIT © Damian Polak