Skip to content
This repository was archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
allow specification of abbreviation in thousands, millions, billions,…
Browse files Browse the repository at this point in the history
… or trillions

Augmenting the existing syntax of 'a' for abbreviation, the format can additionally specify
  'aK' for thousands,
  'aM' for millions,
  'aB' for billions,
  'aT' for trillions

e.g. numeral(-3222111).format('0,0 aK') --> -3,222 K
  • Loading branch information
Alice Li committed Nov 27, 2013
1 parent 4d0be28 commit 7b2ee4c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
20 changes: 16 additions & 4 deletions numeral.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@
signed = false,
optDec = false,
abbr = '',
abbrK = false, // force abbreviation to thousands
abbrM = false, // force abbreviation to millions
abbrB = false, // force abbreviation to billions
abbrT = false, // force abbreviation to trillions
abbrForce = false, // force abbreviation
bytes = '',
ord = '',
abs = Math.abs(value),
Expand Down Expand Up @@ -253,6 +258,13 @@

// see if abbreviation is wanted
if (format.indexOf('a') > -1) {
// check if abbreviation is specified
abbrK = format.indexOf('aK') >= 0;
abbrM = format.indexOf('aM') >= 0;
abbrB = format.indexOf('aB') >= 0;
abbrT = format.indexOf('aT') >= 0;
abbrForce = abbrK || abbrM || abbrB || abbrT;

// check for space before abbreviation
if (format.indexOf(' a') > -1) {
abbr = ' ';
Expand All @@ -261,19 +273,19 @@
format = format.replace('a', '');
}

if (abs >= Math.pow(10, 12)) {
if (abs >= Math.pow(10, 12) && !abbrForce || abbrT) {
// trillion
abbr = abbr + languages[currentLanguage].abbreviations.trillion;
value = value / Math.pow(10, 12);
} else if (abs < Math.pow(10, 12) && abs >= Math.pow(10, 9)) {
} else if (abs < Math.pow(10, 12) && abs >= Math.pow(10, 9) && !abbrForce || abbrB) {
// billion
abbr = abbr + languages[currentLanguage].abbreviations.billion;
value = value / Math.pow(10, 9);
} else if (abs < Math.pow(10, 9) && abs >= Math.pow(10, 6)) {
} else if (abs < Math.pow(10, 9) && abs >= Math.pow(10, 6) && !abbrForce || abbrM) {
// million
abbr = abbr + languages[currentLanguage].abbreviations.million;
value = value / Math.pow(10, 6);
} else if (abs < Math.pow(10, 6) && abs >= Math.pow(10, 3)) {
} else if (abs < Math.pow(10, 6) && abs >= Math.pow(10, 3) && !abbrForce || abbrK) {
// thousand
abbr = abbr + languages[currentLanguage].abbreviations.thousand;
value = value / Math.pow(10, 3);
Expand Down
8 changes: 7 additions & 1 deletion tests/numeral/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ exports.format = {
[1,'0o','1st'],
[52,'0 o','52 nd'],
[23,'0o','23rd'],
[100,'0o','100th']
[100,'0o','100th'],

// specified abbreviations
[-5444333222111, '0,0 aK', '-5,444,333,222 k'],
[-5444333222111, '0,0 aM', '-5,444,333 m'],
[-5444333222111, '0,0 aB', '-5,444 b'],
[-5444333222111, '0,0 aT', '-5 t']
],
i;

Expand Down

0 comments on commit 7b2ee4c

Please sign in to comment.