Easily format a number to fit in a given space (number of characters).
Tested on IE11+, Safari, Firefox, and Chrome. Compatible with Require.js/AMD and NodeJS.
The formatted number can be generated given a number of characters to fit in. Commas, periods count as half-spaces. If a number cannot be abbreviated, it will be cropped with tildes.
The formaat
constructor takes a number of characters to fit in. Commas, periods count as half-spaces.
// pass in number of characters to fit in
var formaat = formaat(3.5);
Pass in the number to be formatted.
Numbers over a trillion will be formatted as "1T+".
// Generate number
var formatted = formaat.abbrev(8100);
8.1K
A Mocha test suite is available.
npm install
npm test