0

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)

2 Answers2

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));
exphoenee
  • 465
  • 4
  • 11
0

Use the .toFixed method:

console.log((1/3).toFixed(2))
exphoenee
  • 465
  • 4
  • 11
  • This has some unintended side-effects. If you do: `console.log((1 / 3).toFixed(100));` it returns 0.3333333333333333148296162562473909929394721984863281250000000000000000000000000000000000000000000000 Plus it only goes up to 100. – The Code Challenger Apr 18 '22 at 06:26