Wednesday, December 24, 2014

How to round up a number to nearest 10?

floor() will go down.

ceil() will go up.

round() will go to nearest by default.

Divide by 10, do the ceil, then multiply by 10 to reduce the significant digits.

$number = ceil($input / 10) * 10;


$maxX = max($tmpArr);

  if ($maxX >= 100000) {
    #return round($maxX, -5);
    return ceil($maxX / 10000) * 10000;
  }
  elseif ($maxX >= 10000) {
    return ceil($maxX / 1000) * 1000;
    #return round($maxX, -4);
  }
  elseif ($maxX >= 1000) {
    return ceil($maxX / 100) * 100;
    #return round($maxX, -3) + 1000;
  }
  elseif ($maxX >= 100) {
    return ceil($maxX / 10) * 10;
    #return round($maxX, -2);
  }
  elseif ($maxX >= 10) {
    return ceil($maxX / 1) * 1;
    #return round($maxX, -1);
  }

  return $maxX;


http://stackoverflow.com/questions/1619265/how-to-round-up-a-number-to-nearest-10

No comments: