Converts numbers into a more human-friendly format. E.g. 123456 becomes 123k. Similar to `ls -lh` or Stack Overflow's reputation numbers.
Abbreviates numbers into a more human-friendly format, similar to ls
's --human-readable
flag (ls -lh
) or Stack
Overflow's reputation numbers. For example, 123456 becomes '123k'.
Works in Node.js and in browsers.
var approx = require('approximate-number');
console.log(approx(1234));
//> 1.2k
console.log(approx(12345));
//> 12k
console.log(approx(1234, {decimal: false}));
//> 1k
console.log(approx(1234, {decimal: ','}));
//> 1,2k
console.log('My Stack Overflow reputation is %s.', approx(3671, {
min10k: true
}));
//> My Stack Overflow reputation is 3,671.
console.log('The US national debt is %s.', approx(19939034457936, {
prefix: '$',
capital: true,
round: true
}));
// > The US national debt is $20T.
// Truncates/floors numbers by default
approx(9999);
// > '9.9k'
// Set `round: true` to round instead of floor.
approx(9999,{round: true});
// > '10k'
Install node module with: npm install --save approximate-number
var approx = require('approximate-number');
approx(123456) // 123k
Install with: bower install approximate-number
<script src="/bower_components/approximate-number/lib/approximate-number.js"></script>
<script>
alert(approximateNumber(1234567890)); // 1.2b
</script>
Grab the latest approximate-number.js and copy it onto your site.
','
. Thousands separator - set to a string (e.g. '.') to use that string or false to not use any separator.'.'
. Decimal - set to a string (e.g. ',') to use that or set to false to avoid outputting values with a decimal.false
. Round numbers off rather than flooring/truncating. When true, 105000 would become '11m', when false it becomes '10m'.false
. Do not abbreviate numbers below 10000. E.g. 9999 would become '9,999' rather than '9k'. (Stack Overflow-style).''
. Optional string to prepend to the value, e.g. '$'.''
. Optional string to append to the value, e.g. '%'.false
. Set to true to use capital letters, e.g. 3.9M instead of 3.9moptions.round=true
.options.min10k=true
.Copyright (c) 2014 Nathan Friedly Licensed under the MIT license.