Skip to content
This repository was archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Fixes problem with many optional decimals
Browse files Browse the repository at this point in the history
When using a format with many optional decimals, insignificant trailing
digits could be added to the number:
Ex:
numeral(3162.63).format('0.0[000000000000]') = "3162.6300000000001"

Also changed numerals internal toFixed method to never return
trailing decimal points.

Also fixed a bug where an extra optional decimal was being added when
using a specified abbreviation.
  • Loading branch information
JPricey authored and Phil Nachum committed Oct 10, 2015
1 parent 7487acb commit 20149c2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion min/numeral.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 21 additions & 10 deletions numeral.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,29 @@
* Fixes binary rounding issues (eg. (0.615).toFixed(2) === '0.61') that present
* problems for accounting- and finance-related software.
*/
function toFixed (value, precision, roundingFunction, optionals) {
var power = Math.pow(10, precision),
function toFixed (value, maxDecimals, roundingFunction, optionals) {
var splitValue = value.toString().split('.'),
minDecimals = maxDecimals - (optionals || 0),
boundedPrecision,
optionalsRegExp,
power,
output;


// Use the smallest precision value possible to avoid errors from floating point representation
if (splitValue.length === 2) {
boundedPrecision = Math.min(Math.max(splitValue[1].length, minDecimals), maxDecimals);
} else {
boundedPrecision = minDecimals;
}

power = Math.pow(10, boundedPrecision);

//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);
output = (roundingFunction(value * power) / power).toFixed(boundedPrecision);

if (optionals) {
optionalsRegExp = new RegExp('0{1,' + optionals + '}$');
if (optionals > maxDecimals - boundedPrecision) {
optionalsRegExp = new RegExp('\\.?0{1,' + (optionals - (maxDecimals - boundedPrecision)) + '}$');
output = output.replace(optionalsRegExp, '');
}

Expand Down Expand Up @@ -276,11 +288,10 @@
// check for space before abbreviation
if (format.indexOf(' a') > -1) {
abbr = ' ';
format = format.replace(' a', '');
} else {
format = format.replace('a', '');
}

format = format.replace(new RegExp(abbr + 'a[KMBT]?'), '');

if (abs >= Math.pow(10, 12) && !abbrForce || abbrT) {
// trillion
abbr = abbr + languages[currentLanguage].abbreviations.trillion;
Expand Down Expand Up @@ -357,7 +368,7 @@

w = d.split('.')[0];

if (d.split('.')[1].length) {
if (d.indexOf('.') > -1) {
d = languages[currentLanguage].delimiters.decimal + d.split('.')[1];
} else {
d = '';
Expand Down
6 changes: 5 additions & 1 deletion tests/numeral/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ exports.format = {
[-0.23,'(.00)','(.23)'],
[0.23,'0.00000','0.23000'],
[0.67,'0.0[0000]','0.67'],
[3162.63,'0.0[00000000000000]','3162.63'],
[1.99,'0.[0]','2'],
[1.0501,'0.00[0]','1.05'],
[2000000000,'0.0a','2.0b'],
[1230974,'0.0a','1.2m'],
[1460,'0a','1k'],
Expand All @@ -70,7 +73,8 @@ exports.format = {
[-5444333222111, '0,0 aK', '-5,444,333,222 k'],
[-5444333222111, '0,0 aM', '-5,444,333 m'],
[-5444333222111, '0,0 aB', '-5,444 b'],
[-5444333222111, '0,0 aT', '-5 t']
[-5444333222111, '0,0 aT', '-5 t'],
[123456, '0.0[0] aK', '123.46 k'],
],
i;

Expand Down

0 comments on commit 20149c2

Please sign in to comment.