Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/SepehrAsh/mout into Sepeh…
Browse files Browse the repository at this point in the history
…rAsh-master
  • Loading branch information
roboshoes committed Sep 1, 2021
2 parents eae5ccc + f83b786 commit 1edd6cf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/number/abbreviate.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ define(['./enforcePrecision'], function (enforcePrecision) {
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
Expand All @@ -26,7 +31,9 @@ define(['./enforcePrecision'], function (enforcePrecision) {
} else {
str = enforcePrecision(val / 1000000000, nDecimals) + dict.billion;
}

if (negative){
str = '-' + str;
}
return str;
}

Expand Down
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 1edd6cf

Please sign in to comment.