jwk811 Posted February 21, 2007 Share Posted February 21, 2007 I need a function that will definently round up to the next 10 if its 0.77 it needs to return 10 if its 50 it would need to return 60 I also need a function that will do the same exept round only down but with one difference.. 50 should return 50 not 40.. and 0.77 would return 0 those are just examples the numbers will vary.. thanks for any help i just cant figure this one out Quote Link to comment https://forums.phpfreaks.com/topic/39514-round-up-and-down-10/ Share on other sites More sharing options...
Ninjakreborn Posted February 21, 2007 Share Posted February 21, 2007 I am understanding a little bit about what you mean, however not all of it. Could you please rephrase the question a little better and give some better examples. Quote Link to comment https://forums.phpfreaks.com/topic/39514-round-up-and-down-10/#findComment-190647 Share on other sites More sharing options...
effigy Posted February 21, 2007 Share Posted February 21, 2007 The same principle from this post should apply. Quote Link to comment https://forums.phpfreaks.com/topic/39514-round-up-and-down-10/#findComment-190659 Share on other sites More sharing options...
ted_chou12 Posted February 21, 2007 Share Posted February 21, 2007 but I think the rounding 50 to 60 part is different... Ted Quote Link to comment https://forums.phpfreaks.com/topic/39514-round-up-and-down-10/#findComment-190668 Share on other sites More sharing options...
obsidian Posted February 21, 2007 Share Posted February 21, 2007 The same principle from this post should apply. Just a little to add here: along with the thread that effigy posted, check out the manual for round(), ceil() and floor() to customize the procedure to your needs. Quote Link to comment https://forums.phpfreaks.com/topic/39514-round-up-and-down-10/#findComment-190669 Share on other sites More sharing options...
obsidian Posted February 21, 2007 Share Posted February 21, 2007 but I think the rounding 50 to 60 part is different... Ted If you're doing rounding of any sort (round, floor or ceil) with values incrementing in 10's, multiples of that base should not ever change. Rereading the OP gave me the same concern, Ted. I think we may be looking at more than simple rounding patterns here. Quote Link to comment https://forums.phpfreaks.com/topic/39514-round-up-and-down-10/#findComment-190670 Share on other sites More sharing options...
ted_chou12 Posted February 21, 2007 Share Posted February 21, 2007 function round10($number) { $number = round($number / 10) * 10; if (($number % 10) == 0) {$number = $number +10;} return $number;} Extracted from the other thread, added so the 50 to 60 is solved. Ted edited; hows this obsidian? Quote Link to comment https://forums.phpfreaks.com/topic/39514-round-up-and-down-10/#findComment-190673 Share on other sites More sharing options...
obsidian Posted February 21, 2007 Share Posted February 21, 2007 function round10($number) { $number = round($number / 10) * 10; if (($number % 10) == 0) {$number = $number +10;} return $number;} Extracted from the other thread, added so the 50 to 60 is solved. Ted edited; hows this obsidian? Here's the problem: <?php // this method will ALWAYS return a rounded number + 10 function round10($number = 45) { // default to 45 $number = round($number / 10) * 10; // now we have 50 if (($number % 10) == 0) $number += 10; // now we have 60 } ?> If you want to account for the initial 10's returning an incremented value, try this: <?php function round10($number) { if ($number % 10 == 0) return ($number + 10); else return round($number / 10) * 10; } ?> I think that would be more realistic. Quote Link to comment https://forums.phpfreaks.com/topic/39514-round-up-and-down-10/#findComment-190695 Share on other sites More sharing options...
ted_chou12 Posted February 21, 2007 Share Posted February 21, 2007 ah, thanks, , I see how it works now. Ted Quote Link to comment https://forums.phpfreaks.com/topic/39514-round-up-and-down-10/#findComment-190699 Share on other sites More sharing options...
jwk811 Posted February 21, 2007 Author Share Posted February 21, 2007 actually forget the 50 to 60 thing.. how could i figure out if its a 10s number like 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140... what im going to do is if its not a number in the 10s i will round to the 10 above on one function and the 10 below on the other so i need the above function to make 51 = 60, 58=60 like that, the down will have to do 51 = 50, 58=50.. so two functions should return: $above = 60; $below = 50; actually thats all the functions have to do.. instead i will only round the two if the number is not in the 10s so two things how can i figure out if its in the 10s and how will i round with the up function and the down function? thanks Quote Link to comment https://forums.phpfreaks.com/topic/39514-round-up-and-down-10/#findComment-190746 Share on other sites More sharing options...
obsidian Posted February 21, 2007 Share Posted February 21, 2007 Try something like this: <?php function roundUpByTen($num) { return ceil($num / 10) * 10; } function roundDownByTen($num) { return floor($num / 10) * 10; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/39514-round-up-and-down-10/#findComment-190769 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.