'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 1:[1]

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));

Solution 2:[2]

Use the .toFixed method:

console.log((1/3).toFixed(2))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 exphoenee
Solution 2 exphoenee