Reputation: 7233
This is my code:
$number = 99.99;
$randomNumber = rand(1, 100);
if($randomNumber > $number) {
// ....
}
Could it be that comparing those numbers, the decimal part gets cut off from the 99.99? It seems that its comparing $randomNumber with 99. Could that be and if yes, how can I compare to 99.99?
Thanks!
Upvotes: 1
Views: 90
Reputation: 2473
I would personally try to do something like this
$number = 99.99
$randomNumber = rand(1, 10000);
if ($randomNumber > number * 100)
{
// ....
}
It could do the trick.
Upvotes: 0
Reputation: 7900
The problem is that rand
returns an int
, then PHP will convert $number
to an int
to make the comparison.
What you should really do is simply cast it's value to float
:
$number = 99.99;
$randomNumber = (float) rand(1, 100);
if($randomNumber > $number) {
// ....
}
This solves the problem and you don't need any magic trick.
Upvotes: 1
Reputation: 4110
just explode it:
$number = 99.99;
$new_num=explode('.',$number)
echo $new_num[0];
//output 99
now you can compare it
Upvotes: 0