What is millify?
The millify npm package is used to convert large numbers into human-readable strings by formatting them with appropriate units (e.g., thousands, millions, billions). It is particularly useful for displaying large numbers in a compact and understandable format.
What are millify's main functionalities?
Basic Number Formatting
This feature allows you to convert a large number into a more readable format by appending a unit suffix. In this example, the number 1,234,567 is converted to '1.23M'.
const millify = require('millify');
console.log(millify(1234567)); // Output: '1.23M'
Custom Precision
Millify allows you to specify the precision of the formatted number. In this example, the precision is set to 1, resulting in '1.2M' instead of the default '1.23M'.
const millify = require('millify');
console.log(millify(1234567, { precision: 1 })); // Output: '1.2M'
Custom Units
You can customize the units used for formatting. This example shows how to specify custom units, although the output remains the same as the default units in this case.
const millify = require('millify');
console.log(millify(1234567, { units: ['K', 'M', 'B', 'T'] })); // Output: '1.23M'
Other packages similar to millify
numeral
Numeral.js is a library for formatting and manipulating numbers. It provides a wide range of formatting options, including currency, percentages, and more. Compared to millify, numeral.js offers more comprehensive number formatting capabilities but may be more complex to use for simple human-readable formatting.
humanize-plus
Humanize-plus is a library that provides various utilities for humanizing data, including number formatting. It offers similar functionality to millify for converting numbers into human-readable formats, but also includes additional features like humanizing file sizes and dates.
pretty-bytes
Pretty-bytes is a package specifically designed for converting bytes into human-readable strings. While it focuses on byte sizes, it shares the same goal as millify in making large numbers more understandable. It is more specialized compared to millify, which handles general numbers.
Millify
Converts long numbers
into pretty, human-readable strings
.
Before :unamused: | After :tada: |
---|
2000 | '2K' |
10000 | '10k' |
42500 | '42.5 kg' |
1250000 | '1.25 MB' |
2700000000 | '2.7 bil' |
Install
Get it on npm:
npm install millify
Usage
Command line
$ millify 12345
12.3K
See millify --help
for options.
Programmatically
millify(value: number, options: MillifyOptions)
import millify from "millify";
millify(2500);
millify(1024000, {
precision: 3,
lowercase: true
});
millify(39500, {
precision: 2,
locales: "de-DE"
});
millify(1440000, {
units: ["B", "KB", "MB", "GB", "TB"],
space: true,
});
Options
Name | Type | Default | Description |
---|
precision | number | 1 | Number of decimal places to use |
locales | string | Array<string> | browser language | Formats the number in different languages |
lowercase | boolean | false | Use lowercase abbreviations |
space | boolean | false | Add a space between number and abbreviation |
units | Array<string> | ['', 'K', 'M', 'B', 'T', 'P', 'E'] | Unit abbreviations |