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!! Link to comment https://forums.phpfreaks.com/topic/91462-why-001-shows-as-0-help/ 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; ?> Link to comment https://forums.phpfreaks.com/topic/91462-why-001-shows-as-0-help/#findComment-468558 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 ?> Link to comment https://forums.phpfreaks.com/topic/91462-why-001-shows-as-0-help/#findComment-468561 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?? Link to comment https://forums.phpfreaks.com/topic/91462-why-001-shows-as-0-help/#findComment-468564 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/91462-why-001-shows-as-0-help/#findComment-468635 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.