I want to get a random double number (for example 4.58) and put its digits to three variables - 4 to the first variable, 5 to the second variable and 8 to the third variable.
-
I need it as double number , to design a program of how to convert meter – Oraib Abo Rob Sep 03 '15 at 10:44
-
http://jsfiddle.net/sherali/coyv3erf/2/ – Sherali Turdiyev Sep 03 '15 at 11:23
3 Answers
Not sure why you need this to be a floating-point number. Just create a three-digit number, convert it to a string, and split it into an array.
var numArr = (Math.floor(Math.random() * 900) + 100).toString().split('');
You can get at the numbers using the normal array method: numArr[0]
etc.
To convert it to number, add a period in the first array position and then join it back to together:
numArr.splice(1, 0, '.');
var number = numArr.join('');
Alternatively, see this SO question on how to create random floating-point numbers.
You could do something like this:
var number = 4.26; // Your generated double number
output = []; // Array to store each digit
sNumber = number.toString(); // Convert the double to a string so we can split it
for (var i = 0, len = sNumber.length; i < len; i += 1)
{
output.push(+sNumber.charAt(i));
}
console.log(output);
The output will be:
4, 2, 6
All numbers in JavaScript are doubles: that is, they are stored as 64-bit IEEE-754 doubles.
That is, the goal is not to get a "double": the goal is to get the string reprsentation of a number formatted as "YYY.XX". For that, consider Number.toFixed, for instance:
(100).toFixed(2)
The result is the string (not a "double"!) "100.00". The parenthesis are required to avoid a grammar ambiguity in this case (it could also have been written as 100.0.toFixed or 100..toFixed), but would not be required if 100 was in a variable.

- 2,501
- 2
- 17
- 25
-
thanks , but , how to get it randomly ? and put a (,) after the first number? – Oraib Abo Rob Sep 03 '15 at 10:48
-
-
-
but the users above , say i can get a random number with 3-digit number but i want to display it like this(4.58) for example – Oraib Abo Rob Sep 03 '15 at 11:01
-
Andy has it right. Also just to note that all numbers generated in JS are Double :) – AnonDCX Sep 03 '15 at 11:05
Use this. I use .replace(/[.]/g,"")
for removing ".".
http://jsfiddle.net/sherali/coyv3erf/2/
var randomNumber = Math.floor(Math.random() * 900) + 100;
numArr= randomNumber.toString().replace(/[.]/g,"").split("")
var number = numArr.join("");
console.log(numArr, number); // ["8", "4", "5"] 845

- 1,745
- 16
- 29
-
thanks :) the reason behind this question , is , i want to make an exercise for children to compute the meter in unites , so if the child press "new problem" a random float number displayed in text , and if he press "solution button" i want to take the numbers of the float- number to show him the solution – Oraib Abo Rob Sep 06 '15 at 18:11
-
@Oraib Abo Rob. I have edited my answer. There are three variables(randomNumber, numArr, number) for using – Sherali Turdiyev Sep 06 '15 at 19:50
-
Accept correct answer, if find you answer. Otherwise, explain it what you wish – Sherali Turdiyev Sep 08 '15 at 17:06