AV1611 Posted September 5, 2008 Share Posted September 5, 2008 I need to round up to the nearest dollar so for example $51,000.01 would be 52,000.00 Doesn't seem to be a way? do I need to add like $499.99 to the value then round to the nearest instead? or is there an easier way? Thanks. Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 5, 2008 Share Posted September 5, 2008 add half a unit (whatever you want to round to) then round down... Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 5, 2008 Share Posted September 5, 2008 That's not rounding up by dollars, it's rounding up by thousands. You could do: <?php $val = 51000.01; $newval = ceil($val * pow(10, -3)) * pow(10, 3); echo $newval; ?> Yay math! And to add the .00 back, just use like, number_format(). Quote Link to comment Share on other sites More sharing options...
AV1611 Posted September 5, 2008 Author Share Posted September 5, 2008 just checking... how can I make it a function (I suck at functions...) ty. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 5, 2008 Share Posted September 5, 2008 <?php function ceil_nearest($number, $power) { if ($power < 1) { return false; } return ceil($number * pow(10, ($power * -1))) * pow(10, $power); } All you need to know is powers of 10. Quote Link to comment Share on other sites More sharing options...
dezkit Posted September 5, 2008 Share Posted September 5, 2008 All you need to know is powers of 10. Is there a article about that on php.net ? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 5, 2008 Share Posted September 5, 2008 I think an 8th grade algebra class would suffice, dezkit. If you really want me to explain them, PM me. =/ Quote Link to comment Share on other sites More sharing options...
AV1611 Posted September 5, 2008 Author Share Posted September 5, 2008 <?php function ceil_nearest($number, $power) { if ($power < 1) { return false; } return ceil($number * pow(10, ($power * -1))) * pow(10, $power); } All you need to know is powers of 10. thank you Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 5, 2008 Share Posted September 5, 2008 Change it to: <?php function ceil_nearest($number, $power) { if ($power < 1) { return false; } elseif (pow(10, $power) > $number) { return pow(10, $power); } return ceil($number * pow(10, ($power * -1))) * pow(10, $power); } Quote Link to comment Share on other sites More sharing options...
jordanwb Posted September 5, 2008 Share Posted September 5, 2008 Is there a article about that on php.net ? Why would there be an article on this? I think an 8th grade algebra class would suffice, dezkit. If you really want me to explain them, PM me. =/ Owned. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 5, 2008 Share Posted September 5, 2008 Is there a article about that on php.net ? Why would there be an article on this? I think an 8th grade algebra class would suffice, dezkit. If you really want me to explain them, PM me. =/ Owned. Lol, I know, right? xD Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 5, 2008 Share Posted September 5, 2008 give this a try: http://phpsnips.com/snippet.php?id=58 Quote Link to comment Share on other sites More sharing options...
jordanwb Posted September 5, 2008 Share Posted September 5, 2008 ^ +1 for that one. 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.