0

I was trying to obtain a random number rounded off to its first decimal place. I got what I wanted with the following code,

(Math.floor(Math.random() * 100) / 10)

But when trying to deduct a value from the output, I had unexpected decimals showing up for some numbers.

(Math.floor(Math.random() * 100) / 10) - 1 //When deducting the problem occurs. 

Any clues as to why this is happening?

Sample output in jsfiddle

satilog
  • 310
  • 2
  • 11

1 Answers1

0

Use Math.round

var number = document.getElementById("num");
setInterval(function() {
   number.innerHTML = Math.round(Math.floor(Math.random() * 100) / 10) - 1;
 }, 1000);
<p id="num" style="color:black">asd</p>
brk
  • 48,835
  • 10
  • 56
  • 78