package
Converts a number to/from a human readable string: `1337` ↔ `1.34 kB`
npm install human-formatGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
23.3 kB
Includes dependencies.
Converts a number to/from a human readable string:
1337↔1.34 kB
Installation of the npm package:
> npm install human-formatThen require the package:
var humanFormat = require("human-format");TypeScript types are included.
You can directly use the build provided at unpkg.com:
<script src="https://unpkg.com/human-format@1/index.js"></script>humanFormat(1337);
//=> '1.34 k'
// The maximum number of decimals can be changed.
humanFormat(1337, {
maxDecimals: 1,
});
//=> '1.3 k'
// maxDecimals can be set to auto, so that there is 1 decimal between -10 and 10 excluded and none out of this interval.
humanFormat(1337, {
maxDecimals: "auto",
});
//=> '1.3 k'
humanFormat(13337, {
maxDecimals: "auto",
});
//=> '13 k'
// Values are rounded to the nearest by default, but can be rounded down (floor)
// or up (ceil) instead.
humanFormat(1390, {
maxDecimals: 1
humanFormat.parse("1.34 KiB", { scale: "binary" });
//=> 1372.16
// Fallbacks when possible if the prefix is incorrectly cased.
humanFormat.parse("1 g");
// => 1000000000
// Exponent notation is supported.
humanFormat.parse("1.5e3 k");
// => 1500000
// If a unit is specified, it is optional but no other unit is accepted.
humanFormat.parse("1 km", { unit: "m" });
// => 1000
humanFormat.parse("5 apples", { unit: "B" });
// throws Error: cannot parse str
// Byte sizes: SI (kB = 1000) and binary (KiB = 1024) prefixes, any case, B
// optional, other units rejected. Bits are not supported.
humanFormat.parse.bytes(
| Option | Default | Description |
|---|---|---|
maxDecimals | 2 (none for raw()) | Maximum number of decimals, or "auto" |
decimals | Fixed number of decimals, overrides maxDecimals | |
rounding | "round" | "round", "floor" or "ceil" |
separator | " " | Between the value and the prefix+unit |
unit | "" ("B" for bytes() and parse.bytes()) | Appended when formatting; when parsing, optional but no other unit is accepted |
scale | "SI" ("binary" for bytes(), both for ) |
Errors:
TypeError if the value to format is not a number, or the string to parse is not a stringError with the message missing scale, invalid prefix, invalid rounding or cannot parse strContributions are very welcomed, either on the documentation or on the code.
You may:
Contributors:
ISC © Julien Fontanet
marsaud
marsaud.fabrice@neuf.fr
parse.bytes()"SI", "binary" or a Scale |
prefix | Forces this prefix when formatting |
strict | false | When parsing, rejects incorrectly cased prefixes (and unit for parse.bytes()) |