hrn is short for Human Readable Number, a simple javascript for browserjs / nodejs library to format number into human-readable string.
npm install --save hrn
var hrn = require('hrn');
//or
import hrn from 'hrn';
There is only one API named hrn(number, fixed, formatter)
.
hrn(1234000); // '1.2 M'
hrn(1234000, 3); // '1.234 M'
hrn(1234000, 3, 'en'); // '1.234 M'
hrn(12340, 2, 'zh_CN'); // '1.23 δΈ', `en` / `zh_CN` supported, `en` is default.
You can customize the number formatter.
// format number
var formatter = ['kmgtpezy'.split(''), 1e3];
hrn('1234000', 1, formatter) // '1.2 m'
// format time diff
formatter = [['s', 'm', 'h', 'd'], [1, 60, 60, 24]];
hrn(23, 1, formatter); // '23.0 s' -> 23 seconds
hrn(23 * 60, 1, formatter); // '23.0 m' -> 23 minutes
hrn(23 * 60 * 60, 1, formatter); // '23.0 h' -> 23 hours
hrn(23 * 60 * 60 * 24, 1, formatter); // '23.0 d' -> 23 days
npm install
npm test
MIT@hustcc