Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Convert a bytes value to a more human-readable format. Choose between metric or IEC units.
The byte-size npm package is a utility for converting bytes into human-readable format. It helps in formatting byte values into strings with appropriate units (e.g., KB, MB, GB) and provides options for customization.
Basic Conversion
This feature allows you to convert a byte value into a human-readable format. In this example, 1024 bytes are converted to 1 KB.
const byteSize = require('byte-size');
const result = byteSize(1024);
console.log(result); // { value: 1, unit: 'KB', long: '1 kilobyte' }
Custom Units
This feature allows you to specify custom units for the conversion. In this example, 1500 bytes are converted to 1.46 KiB using IEC units.
const byteSize = require('byte-size');
const result = byteSize(1500, { units: 'iec' });
console.log(result); // { value: 1.46, unit: 'KiB', long: '1.46 kibibytes' }
Decimal Places
This feature allows you to set the number of decimal places for the conversion. In this example, 123456789 bytes are converted to 117.74 MB with 2 decimal places.
const byteSize = require('byte-size');
const result = byteSize(123456789, { precision: 2 });
console.log(result); // { value: 117.74, unit: 'MB', long: '117.74 megabytes' }
The filesize package is another utility for converting bytes into human-readable strings. It offers more customization options compared to byte-size, such as different rounding methods and support for various notations (e.g., binary, decimal).
The pretty-bytes package is a lightweight utility for converting bytes to a human-readable string. It is simpler and more straightforward than byte-size, focusing on ease of use and minimal configuration.
The bytes package provides utilities for parsing and formatting byte values. It is more versatile than byte-size, offering both conversion to human-readable strings and parsing from strings back to byte values.
Convert a bytes value to a more human-readable format. Choose between metric or IEC units, summarised below.
Value | Metric |
---|---|
1000 | kB kilobyte |
1000^2 | MB megabyte |
1000^3 | GB gigabyte |
1000^4 | TB terabyte |
1000^5 | PB petabyte |
1000^6 | EB exabyte |
1000^7 | ZB zettabyte |
1000^8 | YB yottabyte |
Value | IEC |
---|---|
1024 | KiB kibibyte |
1024^2 | MiB mebibyte |
1024^3 | GiB gibibyte |
1024^4 | TiB tebibyte |
1024^5 | PiB pebibyte |
1024^6 | EiB exbibyte |
1024^7 | ZiB zebibyte |
1024^8 | YiB yobibyte |
string
⏏Kind: Exported function
Param | Type | Default | Description |
---|---|---|---|
bytes | number | the bytes value to convert | |
[options] | object | optional config | |
[options.precision] | number | 1 | number of decimal places |
[options.units] | string | "metric" | select "metric" or "iec" units |
Example
> var byteSize = require("byte-size")
> byteSize(1580)
'1.6 kB'
> byteSize(1580, { units: 'iec' })
'1.5 KiB'
> byteSize(1580, { units: 'iec', precision: 3 })
'1.543 KiB'
> byteSize(1580, { units: 'iec', precision: 0 })
'2 KiB'
© 2015 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.
FAQs
Convert a bytes or octets value (e.g. 34565346) to a human-readable string ('34.6 MB'). Choose between metric or IEC units.
The npm package byte-size receives a total of 1,638,039 weekly downloads. As such, byte-size popularity was classified as popular.
We found that byte-size demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.