CanBeyNABDINIZ
CanBeyNABDINIZ

Reputation: 21

How to generate a random number but not integer and not like 1.2321312312

I want to generate random number but not integer and not like 2.213124125 i want like that: 2.45 2.69 4.52

max 2 number after dot.

Also it returns the old value when i try to click another button.

  var buton = document.createElement("button");
  buton.innerHTML = "Tıkla";
buton.onclick = function myFunction() {
    var x = document.getElementsByClassName("has-input")[1];
    x.value = Math.random(Math.random() * 100) + 2;
}

  document.getElementsByClassName("place_bet m_t_30 bbb manual_bet_select")[0].appendChild(buton);

It generates 2.213123123123 but i want 2.21

And when i click to "btn btn_green" button (in site) it returns to the old value.

Upvotes: 0

Views: 116

Answers (2)

Maheer Ali
Maheer Ali

Reputation: 36594

//This is using without string conversion
let num1 =  Math.floor(Math.random() * 10) + (Math.floor(Math.random() * 100)/100)
console.log(num1)
//This is using 'toFixed()'
let num2 = (Math.random() + (Math.random() * 10)).toFixed(2)
console.log(num2)

Upvotes: -2

Nick H
Nick H

Reputation: 11535

The toFixed method of numbers can return a string with the desired number of decimal places.

(Math.random() + 2).toFixed(2)
"2.96"

Upvotes: 3

Related Questions