1

Suppose I have the number "13.57916667" in a variable $hours. This is a calculated hours combining minutes and seconds to hours.

I want the decimals to round up. For example, I want $hours to become 13.6. I can't seem to find any solution anywhere, even on stackoverflow. Thanks in advance!

cssExp
  • 43
  • 5

4 Answers4

3

Well, this little snippet should do it:

echo round(13.579, 1);

If you're looking for a ceil() with precision:

function ceil_with_precision($value, $precision = 0) {
    return ceil($value * pow(10, $precision)) / pow(10, $precision);
}

For a fixed precision of 1 decimal, that would become:

ceil($value * 10 ) / 10;
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • I understand that he wants the decimals ever rounded up. With this `round(13.02, 1);` you'll get `13` instead `13.1`. – Fabio Mora Aug 14 '12 at 10:33
  • This works great! I modified another code I come across based on the one you provided, What's the difference between: `return (ceil (pow (10, $precision) * $value) + ceil(pow (10, $precision) * $value - ceil (pow (10, $precision) * $value)))/pow (10, $precision);` AND `return ceil($value * pow(10, $precision)) / pow(10, $precision);` Both return the same, is one more accurate than the other? – cssExp Aug 14 '12 at 11:40
  • @cssExp I just see more code is all :) I don't see how the other is more accurate so I'd go with less code :) – Ja͢ck Aug 14 '12 at 12:03
  • I guess that's that! Thank you very much. – cssExp Aug 14 '12 at 12:17
0

$result = round($hours, 1);

See round()

Resurgent
  • 525
  • 2
  • 9
  • 20
0

Try this

$roundednumber= round($number / 10, 0) * 10;
WatsMyName
  • 4,240
  • 5
  • 42
  • 73
0
$hours = (float) "13.57916667";
$hours = ceil($hours * 10) / 10;
Fabio Mora
  • 5,339
  • 2
  • 20
  • 30