Skip to content

Commit

Permalink
Fix formatting of small floating point number
Browse files Browse the repository at this point in the history
  • Loading branch information
eradman committed Jan 29, 2024
1 parent 7de892f commit 4a26dbc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/numeral.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@
power = Math.pow(10, boundedPrecision);

// Multiply up by precision, round accurately, then divide and use native toFixed():
output = (roundingFunction(value + 'e+' + boundedPrecision) / power).toFixed(boundedPrecision);
var valueToRound = value.toString().indexOf('e') >= 0 ? (value * power) : (value + 'e+' + boundedPrecision);
output = (roundingFunction(valueToRound) / power).toFixed(boundedPrecision);

if (optionals > maxDecimals - boundedPrecision) {
optionalsRegExp = new RegExp('\\.?0{1,' + (optionals - (maxDecimals - boundedPrecision)) + '}$');
Expand Down
5 changes: 4 additions & 1 deletion tests/numeral.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ describe('Numeral', function() {
[-5444333222111, '0,0 ab', '-5,444 b'],
[-5444333222111, '0,0 at', '-5 t'],
[123456, '0.0[0] ak', '123.46 k'],
[150,'0.0 ak','0.2 k']
[150,'0.0 ak','0.2 k'],
// tiny floats
[1e-8,'0,0.00000000','0.00000001'],
[1e-8,'0,0.00','0.00']
],
i,
n,
Expand Down

0 comments on commit 4a26dbc

Please sign in to comment.