webguync Posted February 24, 2009 Share Posted February 24, 2009 Will the following code automatically round up or do I need to add 'round' into the code? For example I want the result in the database for $pcnt to round up. If it is 89.5 to round up to 90. <td><?php echo (number_format(($pcnt[$i]*100),2).'%'); ?></td> Quote Link to comment https://forums.phpfreaks.com/topic/146696-solved-will-this-code-round-up/ Share on other sites More sharing options...
premiso Posted February 24, 2009 Share Posted February 24, 2009 You will need to add ceil to round it up I believe. But have you tested that code? Generally you should try it before posting and if you have tried it make a note of that. Quote Link to comment https://forums.phpfreaks.com/topic/146696-solved-will-this-code-round-up/#findComment-770156 Share on other sites More sharing options...
rhodesa Posted February 24, 2009 Share Posted February 24, 2009 From http://us3.php.net/manual/en/function.number-format.php#88424 this is how number_format() rounds: <?php $numbers = array(0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009); foreach ($numbers as $number) print $number."->".number_format($number, 2, '.', ',')."<br>"; ?> 0.001->0.00 0.002->0.00 0.003->0.00 0.004->0.00 0.005->0.01 0.006->0.01 0.007->0.01 0.008->0.01 0.009->0.01 Quote Link to comment https://forums.phpfreaks.com/topic/146696-solved-will-this-code-round-up/#findComment-770157 Share on other sites More sharing options...
JonnoTheDev Posted February 24, 2009 Share Posted February 24, 2009 You need to use round() to round up. $num = 8.95; $num = round($num); ceil rounds fractions up so 4.3 will become 5 Quote Link to comment https://forums.phpfreaks.com/topic/146696-solved-will-this-code-round-up/#findComment-770158 Share on other sites More sharing options...
rhodesa Posted February 24, 2009 Share Posted February 24, 2009 after reading your original post again, it looks like you shouldn't be using number_format() at all. just use round() if you want natural rounding(<.5 down >=.5 up), or ceil() if you want everything to round up Quote Link to comment https://forums.phpfreaks.com/topic/146696-solved-will-this-code-round-up/#findComment-770160 Share on other sites More sharing options...
premiso Posted February 24, 2009 Share Posted February 24, 2009 ceil rounds fractions up so 4.3 will become 5 Yep, you are right. Sorry, my brain is dead tired today lol! Thanks for correcting me, however. Quote Link to comment https://forums.phpfreaks.com/topic/146696-solved-will-this-code-round-up/#findComment-770173 Share on other sites More sharing options...
webguync Posted February 24, 2009 Author Share Posted February 24, 2009 yep, using ROUND works as I want, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/146696-solved-will-this-code-round-up/#findComment-770210 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.