php - Comparing a number to a floating point number? -
this code:
$number = 99.99; $randomnumber = rand(1, 100); if($randomnumber > $number) { // .... }
could comparing numbers, decimal part gets cut off 99.99? seems comparing $randomnumber 99. , if yes, how can compare 99.99?
thanks!
the problem rand
returns int
, php convert $number
int
make comparison.
what should cast it's value float
:
$number = 99.99; $randomnumber = (float) rand(1, 100); if($randomnumber > $number) { // .... }
this solves problem , don't need magic trick.
Comments
Post a Comment