Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toStringWithDecimalPlaces throws RangeError on zero #22

Open
Bazinga9000 opened this issue Jan 2, 2023 · 2 comments
Open

toStringWithDecimalPlaces throws RangeError on zero #22

Bazinga9000 opened this issue Jan 2, 2023 · 2 comments
Labels
awaiting confirmation bug Something isn't working

Comments

@Bazinga9000
Copy link

Reproduction Steps:
ExpantaNum.ZERO.toStringWithDecimalPlaces(3,true)
raises the following error:

Uncaught RangeError: toFixed() digits argument must be between 0 and 100
    at Number.toFixed (<anonymous>)
    at decimalPlaces (eval at <anonymous> (test.js:102:12), <anonymous>:1213:31)
    at P.toStringWithDecimalPlaces (eval at <anonymous> (test.js:102:12), <anonymous>:1250:28)
    at <anonymous>:1:17

This works in the console on https://naruyoko.github.io/ExpantaNum.js/test.html, and also still errors when replacing 3 with any other integer.

@Bazinga9000 Bazinga9000 changed the title toStringWithDecimalPlaces throws RangeError toStringWithDecimalPlaces throws RangeError on zero Jan 2, 2023
@James103
Copy link

James103 commented Jan 2, 2023

Can reproduce the error when running new ExpantaNum(x).toStringWithDecimalPlaces(y,true), where x is any value that, after division by 10y, results in a number less than or equal to 10-100.

In particular, new ExpantaNum(1e-97).toStringWithDecimalPlaces(3,true) raises the same error, and so does new ExpantaNum(1e-96).toStringWithDecimalPlaces(4,true).

Likely a bug in the following function:

ExpantaNum.js/ExpantaNum.js

Lines 1207 to 1212 in df78659

var decimalPlaces=function decimalPlaces(value,places){
var len=places+1;
var numDigits=Math.ceil(Math.log10(Math.abs(value)));
var rounded=Math.round(value*Math.pow(10,len-numDigits))*Math.pow(10,numDigits-len);
return parseFloat(rounded.toFixed(Math.max(len-numDigits,0)));
};

@Naruyoko
Copy link
Owner

α 1.4.0

@Naruyoko Naruyoko reopened this Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting confirmation bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants