Skip to content

Commit 1edd6cf

Browse files
committed
Merge branch 'master' of https://github.com/SepehrAsh/mout into SepehrAsh-master
2 parents eae5ccc + f83b786 commit 1edd6cf

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/number/abbreviate.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ define(['./enforcePrecision'], function (enforcePrecision) {
1515
val = enforcePrecision(val, nDecimals);
1616

1717
var str, mod;
18+
var negative = false;
1819

20+
if (val < 0){
21+
val = -val;
22+
negative = true;
23+
}
1924
if (val < 1000000) {
2025
mod = enforcePrecision(val / 1000, nDecimals);
2126
// might overflow to next scale during rounding
@@ -26,7 +31,9 @@ define(['./enforcePrecision'], function (enforcePrecision) {
2631
} else {
2732
str = enforcePrecision(val / 1000000000, nDecimals) + dict.billion;
2833
}
29-
34+
if (negative){
35+
str = '-' + str;
36+
}
3037
return str;
3138
}
3239

tests/spec/number/spec-abbreviate.js

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ define(['mout/number/abbreviate'], function (abbr) {
77
expect( abbr(55 )).toEqual( '0.1K' );
88
expect( abbr(500 )).toEqual( '0.5K' );
99
expect( abbr(910 )).toEqual( '0.9K' );
10+
expect( abbr(-910 )).toEqual( '-0.9K' );
1011

1112
expect( abbr(999 )).toEqual( '1K' );
1213
expect( abbr(999.9 )).toEqual( '1K' );
@@ -16,13 +17,15 @@ define(['mout/number/abbreviate'], function (abbr) {
1617
expect( abbr(1001 )).toEqual( '1K' );
1718
expect( abbr(1100 )).toEqual( '1.1K' );
1819
expect( abbr(5721 )).toEqual( '5.7K' );
20+
expect( abbr(-5721 )).toEqual( '-5.7K' );
1921

2022
expect( abbr(999000 )).toEqual( '999K' );
2123
expect( abbr(999900 )).toEqual( '999.9K' );
2224
expect( abbr(999990 )).toEqual( '1M' ); // round
2325
expect( abbr(999999 )).toEqual( '1M' );
2426
expect( abbr(1000000 )).toEqual( '1M' );
2527
expect( abbr(1000000.1 )).toEqual( '1M' );
28+
expect( abbr(-1000000.1 )).toEqual( '-1M' );
2629
expect( abbr(1000101 )).toEqual( '1M' );
2730
expect( abbr(1100000 )).toEqual( '1.1M' );
2831
expect( abbr(5721000 )).toEqual( '5.7M' );
@@ -37,6 +40,7 @@ define(['mout/number/abbreviate'], function (abbr) {
3740
expect( abbr(1000000001 )).toEqual( '1B' );
3841
expect( abbr(1100000000 )).toEqual( '1.1B' );
3942
expect( abbr(5721000000 )).toEqual( '5.7B' );
43+
expect( abbr(-5721000000 )).toEqual( '-5.7B' );
4044
expect( abbr(9876543210 )).toEqual( '9.9B' ); // round
4145
});
4246

0 commit comments

Comments
 (0)