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
Parsing
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" });// => 1000humanFormat.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(
Options
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 string
Error with the message missing scale, invalid prefix, invalid rounding or cannot parse str
Contributions
Contributions are very welcomed, either on the documentation or on
the code.