Adam03520 Posted September 23, 2007 Share Posted September 23, 2007 Hi, i need help with the round function to round a number up and another down to the next 10.. Example: First number: 32 Second number: 32 I want the first number to be rounded down to the next 10 so the first number after rounded would become 30.. And the second number wants to be rounded up to the next 10 so the second number would be 40 (after rounded).. I have tryed the ceil() function to round it up to the next 10 but it does not work.. Thank you. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 23, 2007 Share Posted September 23, 2007 Divide by ten, then use floor or ceil (depending on if you want up or down). Then multiply by 10. Quote Link to comment Share on other sites More sharing options...
Adam03520 Posted September 23, 2007 Author Share Posted September 23, 2007 Divide by ten, then use floor or ceil (depending on if you want up or down). Then multiply by 10. Well the problem with ceil and floor is they do nothing to the number.. Here is how i tryed it.. I put this code just to test to see if it did make the number go up / down <?php echo "Ceil Two Is: ".ceil(2)."<br />Floor Two Is: ".floor(2); ?> And the out come from that was this.. Ceil Two Is: 2 Floor Two Is: 2 ----------------------------------------- Wait, sorry i just been on php.net and it says on there it rounds it to the whole number, when this is not doing anything because it is an whole number.. My bad >_> Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 23, 2007 Share Posted September 23, 2007 Now try exactly what jesirose suggested with your examples. It works. If you example isn't representative of what you're really trying to do, then provide a more complete explanation of the problem. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 23, 2007 Share Posted September 23, 2007 Because those are integers, which is the intended result. If you pass it 2.4 you will see something...The floor and ceil of 2 are both 2, that is correct. Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 23, 2007 Share Posted September 23, 2007 echo round(32, -1)."<br>"; echo round(38, -1)."<br>"; http://uk3.php.net/manual/en/function.round.php http://uk3.php.net/manual/en/function.ceil.php Quote Link to comment Share on other sites More sharing options...
Adam03520 Posted September 23, 2007 Author Share Posted September 23, 2007 Now try exactly what jesirose suggested with your examples. It works. If you example isn't representative of what you're really trying to do, then provide a more complete explanation of the problem. Ok, here is the code... <?php $level = 32; $num = $level; $num = $num / 10; $num_floor = floor($num); $num_floor = $num_floor * 10; $num2 = $level; $num2 = $num2 / 10; $num_ceil = ceil($num2); $num_ceil = $num_ceil * 10; echo $num_floor.":".$num_ceil; ?> See the $level string is the number and that number can be any number... What i need the $higher string to echo is the level rounded down to the next 10 and the other rounded up to the next 9.. Example: The level is: 32 The out come from this would be 30:39 Another: The level is: 47 The out come from this would be 40:49 You see what i mean? Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 23, 2007 Share Posted September 23, 2007 function smelly($n) { echo round($n-5, -1)."<br>"; echo ((round($n+5, -1))-1)."<br>"; echo "<br>"; } foreach (array(1, 2, 3, 4, 5, 6, 7, 8, 9) as $n) { smelly(10+$n); } Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 23, 2007 Share Posted September 23, 2007 Adam, before you said you wanted it rounded to even 10s, now you say to the next 9...you're not being clear. Quote Link to comment Share on other sites More sharing options...
Adam03520 Posted September 23, 2007 Author Share Posted September 23, 2007 Adam, before you said you wanted it rounded to even 10s, now you say to the next 9...you're not being clear. Yes, because i didnt think you would really understand, and with it been to the 10s it would be easyer for you and then i could just take away one from the value. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 23, 2007 Share Posted September 23, 2007 That's rich. If you want help with something, you should ask for what you want help with. If you assume we won't understand why would we be able to help in the first place? You should divide by 10, use floor, multiply by 10. That is your 30. then ADD 9 and that is your 39. 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.