Skip to content

Commit

Permalink
Merge pull request #107 from andrewla/develop
Browse files Browse the repository at this point in the history
Fixing large number behavior of format
  • Loading branch information
BenjaminVanRyseghem committed Oct 14, 2015
2 parents 7d16321 + 4ab3161 commit 0c2d42c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
50 changes: 47 additions & 3 deletions numbro.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,43 @@
this._value = number;
}

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

for (i = 0; i < count; i++) {
ret += '0';
}

return ret;
}
/**
* Implementation of toFixed() for numbers with exponent > 21
*
*
*/
function toFixedLarge(value, precision) {
var mantissa,
beforeDec,
afterDec,
exponent,
str;

str = value.toString();

mantissa = str.split('e')[0];
exponent = str.split('e')[1];

beforeDec = mantissa.split('.')[0];
afterDec = mantissa.split('.')[1] || '';

str = beforeDec + afterDec + zeroes(exponent - afterDec.length);
if (precision > 0) {
str += '.' + zeroes(precision);
}

return str;
}

/**
* Implementation of toFixed() that treats floats more like decimals
*
Expand All @@ -77,9 +114,16 @@
optionalsRegExp,
output;

//roundingFunction = (roundingFunction !== undefined ? roundingFunction : Math.round);
// Multiply up by precision, round accurately, then divide and use native toFixed():
output = (roundingFunction(value * power) / power).toFixed(precision);
if (value.toFixed(0).search('e') > -1) {
// Above 1e21, toFixed returns scientific notation, which
// is useless and unexpected
output = toFixedLarge(value, precision);
}
else {
//roundingFunction = (roundingFunction !== undefined ? roundingFunction : Math.round);
// Multiply up by precision, round accurately, then divide and use native toFixed():
output = (roundingFunction(value * power) / power).toFixed(precision);
}

if (optionals) {
optionalsRegExp = new RegExp('0{1,' + optionals + '}$');
Expand Down
21 changes: 21 additions & 0 deletions tests/numbro/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ exports.format = {
[18823578.85, '6 a', '18823.6 k'],
[188235773.85, '6 a', '188236 k'],

// large numbers
[100, '0,0[.]0000','100'],
[1e23, '0,0[.]0000','100,000,000,000,000,000,000,000'],

[1e19, '0,0.0000','10,000,000,000,000,000,000.0000'],
[1e20, '0,0.0000','100,000,000,000,000,000,000.0000'],
[1e21, '0,0.0000','1,000,000,000,000,000,000,000.0000'],
[1e22, '0,0.0000','10,000,000,000,000,000,000,000.0000'],
[1e23, '0,0.0000','100,000,000,000,000,000,000,000.0000'],

[-1e19, '0,0.0000','-10,000,000,000,000,000,000.0000'],
[-1e20, '0,0.0000','-100,000,000,000,000,000,000.0000'],
[-1e21, '0,0.0000','-1,000,000,000,000,000,000,000.0000'],
[-1e22, '0,0.0000','-10,000,000,000,000,000,000,000.0000'],
[-1e23, '0,0.0000','-100,000,000,000,000,000,000,000.0000'],

[1.1e23, '0,0.0000','110,000,000,000,000,000,000,000.0000'],
[1.11e23, '0,0.0000','111,000,000,000,000,000,000,000.0000'],
[1.111e23, '0,0.0000','111,100,000,000,000,000,000,000.0000'],


// Non-finite numbers
[Infinity, '0.0', 'Infinity'],
[-Infinity, '0.0', '-Infinity'],
Expand Down

0 comments on commit 0c2d42c

Please sign in to comment.