11 Replies Latest reply on Nov 12, 2006 2:17 PM by twistedpancreas

# Math.random dropping off a decimal place

hello fellow peoples,

got this problem

this.input1.text = Math.round( (0.140 + Math.random() * 0.040) * 1000 ) / 1000;

sometimes this gives an answer of 0.14 or 0.15, etc, etc...

is there anyway to make it not drop off the end 0, ie it should be 0.140, or 0.150 (got a very specific chemistry client)

• ###### 1. Re: Math.random dropping off a decimal place
twistedpancreas,

> is there anyway to make it not drop off the end 0, ie it
> should be 0.140, or 0.150 (got a very specific chemistry
> client)

You'll have to convert that number to a string and add the final "0"
yourself.

David Stiller
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."

• ###### 2. Re: Math.random dropping off a decimal place
// if your answer will never equal or larger than 1, use this:
this.input1.text = "0." + Math.round( (0.140 + Math.random() * 0.040) * 1000 ) ;

// if your answer may be equal or larger than 1, use this:
var num1:Number = Math.round((0.140+Math.random()*0.040)*1000);
this.input1.text = Math.floor(num1/1000)+"."+num1%1000;
• ###### 3. Re: Math.random dropping off a decimal place
> // if your answer may be equal or larger than 1, use this:
> var num1:Number = Math.round((0.140+Math.random()*0.040)*1000);
> this.input1.text = Math.floor(num1/1000)+"."+num1%1000;

That doesn't work (as anywhere near a general answer) .. you're just lucky
in this case that with the particular limited range of numbers wanted, it
will fluke it and give the correct answer.

put them up here and confuse people.
--
Jeckyl

• ###### 4. Re: Math.random dropping off a decimal place
sorry guys.. my mistake here
• ###### 5. Math.random dropping off a decimal place
var num1:Number = 95.0104; // your number
var num2:Number = Math.floor(num1); // without decimal
var num3:Number = Math.round(num1*1000%1000); //3 digit of decimal value
if(num3<10){
} else if (num3<100){
}
• ###### 7. Re: Math.random dropping off a decimal place
sorry if i've confused people but i'll try and explain again in a more simple explaination:

say with my code i get a random number of 0.143 then my client is happy with that (because it's to 3 decimal places)

say if we run the script again and get a random number of 0.15 then my client isn'y happy with that because it isn't strictly to 3 decimal places (yeah i know, picky, but the client knows best :D)

they would rather it include the "0" on the end, making the number 0.150

hopefully thats clearer.
• ###### 8. Math.random dropping off a decimal place
try this:

• ###### 9. Re: Math.random dropping off a decimal place
otherwise you can use like this simply

• ###### 10. Re: Math.random dropping off a decimal place
twistedpancreas,

> say if we run the script again and get a random number of
> 0.15 then my client isn'y happy with that because it isn't
> strictly to 3 decimal places (yeah i know, picky, but the
> client knows best :D)

You betcha. I understand what you're after.

> they would rather it include the "0" on the end, making the
> number 0.150

For better or worse, Flash simply doesn't display numbers that way. In
order to give your client the desired output, you'll have to manually add
zeroes to a string version of your number. You could, for example, use
String.indexOf() to test of the string (the number) has a decimal point at
all. If not, simply append three zeroes. If so, use String.length to check
the total number of characters. Note that number. Now, round the number
with Math.round() and note *that* number. Compare the two numbers, and
you'll know how many zeroes to add.

David Stiller