How to get decimal number upto 4 decimal but no decimal if not necessary

Tell us what’s happening:

i am trying to get decimal number upto 4 decimal but i also want number if there is no need of decimal

ex: 22/7 should result in 3.1428 but 5 + 5 = 10 and not 10.0000

Your code so far

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Build a JavaScript Calculator

Link to the challenge:

You can try the following.

const numberToRound = 3.144499; //do some computation

const roundedValue = Number.isInteger(numberToRound) ? numberToRound :  Math.round(numberToRound * 10000)/10000;
1 Like