Jump to content

round up and down 10


jwk811

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.