Skip to content

Commit

Permalink
Merge branch 'SepehrAsh-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
roboshoes committed Sep 1, 2021
2 parents eae5ccc + 1416dac commit ea3d2d8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/number/abbreviate.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
define(['./enforcePrecision'], function (enforcePrecision) {

define(["./enforcePrecision"], function (enforcePrecision) {
var _defaultDict = {
thousand : 'K',
million : 'M',
billion : 'B'
thousand: "K",
million: "M",
billion: "B",
};

/**
* Abbreviate number if bigger than 1000. (eg: 2.5K, 17.5M, 3.4B, ...)
*/
function abbreviateNumber(val, nDecimals, dict){
nDecimals = nDecimals != null? nDecimals : 1;
function abbreviateNumber(val, nDecimals, dict) {
nDecimals = nDecimals != null ? nDecimals : 1;
dict = dict || _defaultDict;
val = enforcePrecision(val, nDecimals);

var str, mod;
var negative = false;

if (val < 0) {
val = -val;
negative = true;
}

if (val < 1000000) {
mod = enforcePrecision(val / 1000, nDecimals);
// might overflow to next scale during rounding
str = mod < 1000? mod + dict.thousand : 1 + dict.million;
str = mod < 1000 ? mod + dict.thousand : 1 + dict.million;
} else if (val < 1000000000) {
mod = enforcePrecision(val / 1000000, nDecimals);
str = mod < 1000? mod + dict.million : 1 + dict.billion;
str = mod < 1000 ? mod + dict.million : 1 + dict.billion;
} else {
str = enforcePrecision(val / 1000000000, nDecimals) + dict.billion;
}

if (negative) {
str = "-" + str;
}

return str;
}

return abbreviateNumber;

});
4 changes: 4 additions & 0 deletions tests/spec/number/spec-abbreviate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ define(['mout/number/abbreviate'], function (abbr) {
expect( abbr(55 )).toEqual( '0.1K' );
expect( abbr(500 )).toEqual( '0.5K' );
expect( abbr(910 )).toEqual( '0.9K' );
expect( abbr(-910 )).toEqual( '-0.9K' );

expect( abbr(999 )).toEqual( '1K' );
expect( abbr(999.9 )).toEqual( '1K' );
Expand All @@ -16,13 +17,15 @@ define(['mout/number/abbreviate'], function (abbr) {
expect( abbr(1001 )).toEqual( '1K' );
expect( abbr(1100 )).toEqual( '1.1K' );
expect( abbr(5721 )).toEqual( '5.7K' );
expect( abbr(-5721 )).toEqual( '-5.7K' );

expect( abbr(999000 )).toEqual( '999K' );
expect( abbr(999900 )).toEqual( '999.9K' );
expect( abbr(999990 )).toEqual( '1M' ); // round
expect( abbr(999999 )).toEqual( '1M' );
expect( abbr(1000000 )).toEqual( '1M' );
expect( abbr(1000000.1 )).toEqual( '1M' );
expect( abbr(-1000000.1)).toEqual( '-1M' );
expect( abbr(1000101 )).toEqual( '1M' );
expect( abbr(1100000 )).toEqual( '1.1M' );
expect( abbr(5721000 )).toEqual( '5.7M' );
Expand All @@ -37,6 +40,7 @@ define(['mout/number/abbreviate'], function (abbr) {
expect( abbr(1000000001 )).toEqual( '1B' );
expect( abbr(1100000000 )).toEqual( '1.1B' );
expect( abbr(5721000000 )).toEqual( '5.7B' );
expect( abbr(-5721000000 )).toEqual( '-5.7B' );
expect( abbr(9876543210 )).toEqual( '9.9B' ); // round
});

Expand Down

0 comments on commit ea3d2d8

Please sign in to comment.