ivytony Posted February 17, 2008 Share Posted February 17, 2008 I'm a newb in PHP. <?php $rate = 0.01; $rate = ($rate * 100). "%"; echo rate; ?> I got this result from this above code: 0% I also tried to do casting by $rate = ( double ) $rate; but I still get the same result. I am wondering how to get correct value. thanks!! Quote Link to comment Share on other sites More sharing options...
Stooney Posted February 17, 2008 Share Posted February 17, 2008 Works fine for me. In your example you missed the '$' when outputting. So you should have: echo $rate; This code gave me 1% <?php $rate = 0.01; $rate = ($rate * 100). "%"; echo $rate; ?> Quote Link to comment Share on other sites More sharing options...
Chris92 Posted February 17, 2008 Share Posted February 17, 2008 You might want to through the bc manuals on the php.net website, i think it might be like bcmul() or something. should work something like this: <?php echo bcmul('100', '0.01', 3); // should return 1, the three will let it go to 3 decimal places ?> Quote Link to comment Share on other sites More sharing options...
ivytony Posted February 17, 2008 Author Share Posted February 17, 2008 thanks for the replies. When I use $rate = 0.01 for an if statement, I got false results. For example $rate = 0.01; if ($rate > 0) { do A; } else { do B } The running result for this if statement is: do B. But my $rate is 0.01 which is greater than 0, right?? Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted February 17, 2008 Share Posted February 17, 2008 Not sure what the problem would be. Using php 5.2.5 on CentOS 5.1 this returns A. <?php $rate = 0.01; if ($rate > 0) { echo "A"; } else { echo "B"; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.