From e3cc6133690c2ce7f976794baa5e66b785a86690 Mon Sep 17 00:00:00 2001 From: Andrew Laucius Date: Wed, 14 Oct 2015 09:56:06 -0400 Subject: [PATCH 1/2] Adding broken test cases --- tests/numbro/format.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/numbro/format.js b/tests/numbro/format.js index 28360539..3deaabe4 100644 --- a/tests/numbro/format.js +++ b/tests/numbro/format.js @@ -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'], From 4ab3161965567a441a8ba43476586af3ffbfc236 Mon Sep 17 00:00:00 2001 From: Andrew Laucius Date: Wed, 14 Oct 2015 09:52:16 -0400 Subject: [PATCH 2/2] Supporting large numbers `toFixed` does not work properly if the number is greater in absolute value than 1e21. Fixes #106 --- numbro.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/numbro.js b/numbro.js index a411f5bd..2867c0e0 100644 --- a/numbro.js +++ b/numbro.js @@ -35,6 +35,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 * @@ -46,9 +83,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 + '}$');