diff --git a/humanize.js b/humanize.js index 1f5c4f6..f993bd7 100644 --- a/humanize.js +++ b/humanize.js @@ -1,4 +1,3 @@ - (function() { // Baseline setup @@ -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 + * @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).