Skip to content

Commit

Permalink
Fix #190 Allow abbreviation precision for < 4
Browse files Browse the repository at this point in the history
Currently for some reason when using abbreviations like `'0,0a`  you
can't specify the number of visible digits of precision for any value
less than 4. So while `'4,0a'` will change `1234567` to `1,234k` the
result of `'3,0a'` is `1m` instead of `1.23m` as expected.

This is primarily caused by a conditional that skips setting the
precision in the formatting.

This commit changes how the algorithm determines if decimal places will
be required and how many are required. We first compute the abbreviated
unitized value and then use the length of that value to determine how
many decimal places will be required.
  • Loading branch information
chrisnicola committed Oct 20, 2016
1 parent e856c2b commit ce2dbd1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
30 changes: 13 additions & 17 deletions numbro.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
this._value = number;
}

function numberLength(number) {
if (number === 0) { return 1; }
return Math.floor(Math.log(Math.abs(number)) / Math.LN10) + 1;
}

function zeroes(count) {
var i, ret = '';

Expand Down Expand Up @@ -472,7 +477,6 @@
forcedNeg = false,
neg = false,
indexOpenP,
size,
indexMinus,
paren = '',
minlen,
Expand Down Expand Up @@ -551,30 +555,16 @@
format = format.replace('a', '');
}

totalLength = Math.floor(Math.log(abs) / Math.LN10) + 1;

totalLength = numberLength(value);
minimumPrecision = totalLength % 3;
minimumPrecision = minimumPrecision === 0 ? 3 : minimumPrecision;

if (intPrecision && abs !== 0) {

length = Math.floor(Math.log(abs) / Math.LN10) + 1 - intPrecision;

pow = 3 * ~~((Math.min(intPrecision, totalLength) - minimumPrecision) / 3);

abs = abs / Math.pow(10, pow);

if (format.indexOf('.') === -1 && intPrecision > 3) {
format += '[.]';

size = length === 0 ? 0 : 3 * ~~(length / 3) - length;
size = size < 0 ? size + 3 : size;

format += zeroes(size);
}
}

if (Math.floor(Math.log(Math.abs(value)) / Math.LN10) + 1 !== intPrecision) {
if (totalLength !== intPrecision) {
if (abs >= Math.pow(10, 12) && !abbrForce || abbrT) {
// trillion
abbr = abbr + cultures[currentCulture].abbreviations.trillion;
Expand All @@ -593,6 +583,12 @@
value = value / Math.pow(10, 3);
}
}

length = numberLength(value);
if (intPrecision && length < intPrecision && format.indexOf('.') === -1) {
format += '[.]';
format += zeroes(intPrecision - length);
}
}

// see if we are formatting
Expand Down
5 changes: 5 additions & 0 deletions tests/numbro/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ exports.format = {
[123456789, '9 a', '123456789 '],
[1234567891, '10 a', '1234567891 '],

[1234567, '2 a', '1.2 m'],
[1234567, '3 a', '1.23 m'],

[0, '2a', '0'],

[18823.85, '6 a', '18823.9 '],
[188235.85, '6 a', '188236 '],
[1882357.85, '6 a', '1882.36 k'],
Expand Down

0 comments on commit ce2dbd1

Please sign in to comment.