I have a random number generator function that returns a random number. It looks like the problem is that sometimes it returns only one decimal place the, tenths place(i think it's called). I always want it to output a number with 2 decimal places (some times i would get 10.5 when i want 10.50).
in my code i put a plus sign to convert it into a number.I believe this is what is causing the hundredth's place to be deleted. what is the best way to solve this problem?
function randomGen(){
return + (Math.random()*11).toFixed(2)
}
// console.log(randomGen())
// some times i would get 10.5 when i want 10.50
var num1 = randomGen(); // i want 10.50, for example not 10.5
var num2 = randomGen();
console.log(num1, " ", num2)
console.log(+(num1 + num2).toFixed(2))
Thank you
Aucun commentaire:
Enregistrer un commentaire