Skip to content

Commit

Permalink
Merge pull request #147 from MarkHerhold/develop
Browse files Browse the repository at this point in the history
Fixing formatting issues for very small (+/-1e-23) numbers.
  • Loading branch information
BenjaminVanRyseghem committed Apr 11, 2016
2 parents dc02855 + 71a5a65 commit 2635430
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 18 deletions.
52 changes: 41 additions & 11 deletions numbro.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,52 @@
return ret;
}
/**
* Implementation of toFixed() for numbers with exponent > 21
*
*
* Implementation of toFixed() for numbers with exponents
* This function may return negative representations for zero values e.g. "-0.0"
*/
function toFixedLarge(value, precision) {
function toFixedLargeSmall(value, precision) {
var mantissa,
beforeDec,
afterDec,
exponent,
prefix,
endStr,
zerosStr,
str;

str = value.toString();

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

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

str = beforeDec + afterDec + zeroes(exponent - afterDec.length);
if (precision > 0) {
if (+exponent > 0) {
// exponent is positive - add zeros after the numbers
str = beforeDec + afterDec + zeroes(exponent - afterDec.length);
} else {
// exponent is negative

if (+beforeDec < 0) {
prefix = '-0';
} else {
prefix = '0';
}

// tack on the decimal point if needed
if (precision > 0) {
prefix += '.';
}

zerosStr = zeroes((-1 * exponent) - 1);
// substring off the end to satisfy the precision
endStr = (zerosStr + Math.abs(beforeDec) + afterDec).substr(0, precision);
str = prefix + endStr;
}

// only add percision 0's if the exponent is positive
if (+exponent > 0 && precision > 0) {
str += '.' + zeroes(precision);
}

Expand All @@ -110,16 +135,21 @@
*
* Fixes binary rounding issues (eg. (0.615).toFixed(2) === '0.61') that present
* problems for accounting- and finance-related software.
*
* Also removes negative signs for zero-formatted numbers. e.g. -0.01 w/ precision 1 -> 0.0
*/
function toFixed(value, precision, roundingFunction, optionals) {
var power = Math.pow(10, precision),
optionalsRegExp,
output;

if (value.toFixed(0).search('e') > -1) {
// Above 1e21, toFixed returns scientific notation, which
// is useless and unexpected
output = toFixedLarge(value, precision);
if (value.toString().indexOf('e') > -1) {
// toFixed returns scientific notation for numbers above 1e21 and below 1e-7
output = toFixedLargeSmall(value, precision);
// remove the leading negative sign if it exists and should not be present (e.g. -0.00)
if (output.charAt(0) === '-' && +output >= 0) {
output = output.substr(1); // chop off the '-'
}
}
else {
// Multiply up by precision, round accurately, then divide and use native toFixed():
Expand Down
32 changes: 25 additions & 7 deletions tests/numbro/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,26 @@ exports.format = {
[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'],
[NaN, '0.0', 'NaN']

// small numbers - see issue #145
[1e-5, '0','0'],
[1e-5, '0.0','0.0'],
[1e-5, '0.00000','0.00001'],
[1e-5, '0.00000000','0.00001000'],
[1e-23, '0.0','0.0'],
[1e-23, '0.000','0.000'],
[1e-23, '0.00000000000000000000000','0.00000000000000000000001'],
[1.1e-23, '0.000000000000000000000000','0.000000000000000000000011'],
[-0.001, '0.00','0.00'],
[-0.001, '0.000','-0.001'],
[-1e-5, '0.00','0.00'],
[-1e-23, '0.0000000000000000000000','0.0000000000000000000000'],
[-1e-23, '0.00000000000000000000000','-0.00000000000000000000001'],
[-1.1e-23, '0.000000000000000000000000','-0.000000000000000000000011'],

// Non-finite numbers
[Infinity, '0.0', 'Infinity'],
[-Infinity, '0.0', '-Infinity'],
[NaN, '0.0', 'NaN']
],
i;

Expand Down Expand Up @@ -180,7 +194,11 @@ exports.format = {
[1000.234,'$(0,0.00)','$1,000.23'],
[1000.238,'$(0,0.00)','$1,000.24'],
[1000.234,'$-0,0)','$1,000'],
[1000.234,'$ -0,0','$ 1,000']
[1000.234,'$ -0,0','$ 1,000'],

// small numbers - see issue #145
[-0.00000000000001,'$0,0','$0'],
[-0.00000000000001,'$0,0.00','$0.00']
],
i;

Expand Down

0 comments on commit 2635430

Please sign in to comment.