Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion humanize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

(function() {

// Baseline setup
Expand Down Expand Up @@ -377,6 +376,22 @@

return sign + number + (number > 4 && number < 21 ? 'th' : {1: 'st', 2: 'nd', 3: 'rd'}[number % 10] || 'th');
};

//**
* Formats the value based on its (short) scale.
* @example humanize.numberScale(1000000000) // "1 billion"
*
* Thanks to Dmitry Baranovskiy <dmitry@baranovskiy.com>
* @see https://gist.github.com/852326#gistcomment-22591
*/
humanize.numberScale = function(n) {
var nlist = String(Math.abs(n)).replace(/^\d+(?=.|$)/, function(int) { return int.replace(/(?=(?:\d{3})+$)(?!^)/g, ","); }).split(",");
var num = nlist.shift();
var rem = nlist[0] || 0;
var rnd = Math.round(rem/10)

return (n < 0 ? "-" : "") + num + (rnd === 0 ? "" : "." + rnd) + " " + ["","thousand","million","billion","trillion"][nlist.length];
};

/**
* Formats the value like a 'human-readable' file size (i.e. '13 KB', '4.1 MB', '102 bytes', etc).
Expand Down