1 | # pretty-bytes
|
---|
2 |
|
---|
3 | > Convert bytes to a human readable string: `1337` → `1.34 kB`
|
---|
4 |
|
---|
5 | Useful for displaying file sizes for humans.
|
---|
6 |
|
---|
7 | *Note that it uses base-10 (e.g. kilobyte).
|
---|
8 | [Read about the difference between kilobyte and kibibyte.](https://web.archive.org/web/20150324153922/https://pacoup.com/2009/05/26/kb-kb-kib-whats-up-with-that/)*
|
---|
9 |
|
---|
10 | ## Install
|
---|
11 |
|
---|
12 | ```
|
---|
13 | $ npm install pretty-bytes
|
---|
14 | ```
|
---|
15 |
|
---|
16 | ## Usage
|
---|
17 |
|
---|
18 | ```js
|
---|
19 | const prettyBytes = require('pretty-bytes');
|
---|
20 |
|
---|
21 | prettyBytes(1337);
|
---|
22 | //=> '1.34 kB'
|
---|
23 |
|
---|
24 | prettyBytes(100);
|
---|
25 | //=> '100 B'
|
---|
26 |
|
---|
27 | // Display with units of bits
|
---|
28 | prettyBytes(1337, {bits: true});
|
---|
29 | //=> '1.34 kbit'
|
---|
30 |
|
---|
31 | // Display file size differences
|
---|
32 | prettyBytes(42, {signed: true});
|
---|
33 | //=> '+42 B'
|
---|
34 |
|
---|
35 | // Localized output using German locale
|
---|
36 | prettyBytes(1337, {locale: 'de'});
|
---|
37 | //=> '1,34 kB'
|
---|
38 | ```
|
---|
39 |
|
---|
40 | ## API
|
---|
41 |
|
---|
42 | ### prettyBytes(number, options?)
|
---|
43 |
|
---|
44 | #### number
|
---|
45 |
|
---|
46 | Type: `number`
|
---|
47 |
|
---|
48 | The number to format.
|
---|
49 |
|
---|
50 | #### options
|
---|
51 |
|
---|
52 | Type: `object`
|
---|
53 |
|
---|
54 | ##### signed
|
---|
55 |
|
---|
56 | Type: `boolean`\
|
---|
57 | Default: `false`
|
---|
58 |
|
---|
59 | Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment.
|
---|
60 |
|
---|
61 | ##### bits
|
---|
62 |
|
---|
63 | Type: `boolean`\
|
---|
64 | Default: `false`
|
---|
65 |
|
---|
66 | Format the number as [bits](https://en.wikipedia.org/wiki/Bit) instead of [bytes](https://en.wikipedia.org/wiki/Byte). This can be useful when, for example, referring to [bit rate](https://en.wikipedia.org/wiki/Bit_rate).
|
---|
67 |
|
---|
68 | ##### binary
|
---|
69 |
|
---|
70 | Type: `boolean`\
|
---|
71 | Default: `false`
|
---|
72 |
|
---|
73 | Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_prefix). This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes.
|
---|
74 |
|
---|
75 | ##### locale
|
---|
76 |
|
---|
77 | Type: `boolean | string`\
|
---|
78 | Default: `false` *(No localization)*
|
---|
79 |
|
---|
80 | **Important:** Only the number and decimal separator are localized. The unit title is not and will not be localized.
|
---|
81 |
|
---|
82 | - If `true`: Localize the output using the system/browser locale.
|
---|
83 | - If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
|
---|
84 | - If `string[]`: Expects a list of [BCP 47 language tags](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
|
---|
85 |
|
---|
86 | **Note:** Localization should generally work in browsers. Node.js needs to be [built](https://github.com/nodejs/node/wiki/Intl) with `full-icu` or `system-icu`. Alternatively, the [`full-icu`](https://github.com/unicode-org/full-icu-npm) module can be used to provide support at runtime. [Node.js 13](https://nodejs.org/en/blog/release/v13.0.0/) and later ships with ICU by default.
|
---|
87 |
|
---|
88 | ##### minimumFractionDigits
|
---|
89 |
|
---|
90 | Type: `number`\
|
---|
91 | Default: `undefined`
|
---|
92 |
|
---|
93 | The minimum number of fraction digits to display.
|
---|
94 |
|
---|
95 | If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
|
---|
96 |
|
---|
97 | ```js
|
---|
98 | const prettyBytes = require('pretty-bytes');
|
---|
99 |
|
---|
100 | // Show the number with at least 3 fractional digits
|
---|
101 | prettyBytes(1900, {minimumFractionDigits: 3});
|
---|
102 | //=> '1.900 kB'
|
---|
103 |
|
---|
104 | prettyBytes(1900);
|
---|
105 | //=> '1.9 kB'
|
---|
106 | ```
|
---|
107 |
|
---|
108 | ##### maximumFractionDigits
|
---|
109 |
|
---|
110 | Type: `number`\
|
---|
111 | Default: `undefined`
|
---|
112 |
|
---|
113 | The maximum number of fraction digits to display.
|
---|
114 |
|
---|
115 | If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
|
---|
116 |
|
---|
117 | ```js
|
---|
118 | const prettyBytes = require('pretty-bytes');
|
---|
119 |
|
---|
120 | // Show the number with at most 1 fractional digit
|
---|
121 | prettyBytes(1920, {maximumFractionDigits: 1});
|
---|
122 | //=> '1.9 kB'
|
---|
123 |
|
---|
124 | prettyBytes(1920);
|
---|
125 | //=> '1.92 kB'
|
---|
126 | ```
|
---|
127 |
|
---|
128 | ## Related
|
---|
129 |
|
---|
130 | - [pretty-bytes-cli](https://github.com/sindresorhus/pretty-bytes-cli) - CLI for this module
|
---|
131 | - [pretty-ms](https://github.com/sindresorhus/pretty-ms) - Convert milliseconds to a human readable string
|
---|