0

I would liket to use javaScript to generate random number with 3 decimals. using the following code: ${__javaScript((Math.random() * (40) - 20).toFixed(3) * 1,)}

it generate the value -7.829999999999999, but I want the value -7.830.

Anyone knows how do I get what I want? thank you in advance. enter image description hereenter image description here

Joyce Gao
  • 3
  • 3
  • I tried your code, it seems you are doing it the correct way. You should be getting the output you desire. – Prashanth KR Mar 09 '18 at 07:39
  • What you want is apparently a variable that can store the value -7.830 exactly. But this is not possible. In binary, you would need an infinite number of digits to store this number. See [Is floating point math broken?](https://stackoverflow.com/q/588004/1679849) – r3mainer Mar 09 '18 at 08:34
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Pac0 Mar 16 '19 at 17:08

2 Answers2

1

Your solution seems to work..

(Math.random() * 40 - 20).toFixed(3) even like this

Working fiddle here : https://jsfiddle.net/ppgcLf6m/2/

0

Calling parseFloat on the end result should work.

parseFloat((Math.random() * (40) -20).toFixed(3))