Search code examples
javascriptmathinfinity

How to stop Javascript from rounding numbers after 16 decimal places?


Let's say I have the following code:

console.log(1 / 3);

It prints "0.3333333333333333".

Is there a way to not have it stop?

(For any curious souls: it's for calcuating Pi)


Solution

  • Write a function whrere the numbers are multiplied with precision before they divided:

    function div(num1, num2, prec=100) {
      return (num1*prec)/(num2*prec).toFixed(prec)
    }
    
    console.log(div(1,3,100));
    console.log(div(11,13,100));