Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP rounding, round/ceil/floor??

Tags:

php

Say I have the two following numbers:

$a = 77.5;
$b = 74.5;

How would I get the following:

$a = 80;
$b = 70;

I have looked at round, ceil and floor but I can't figure out how to do it.

Thank you.

like image 811
Latox Avatar asked Dec 12 '22 10:12

Latox


2 Answers

PHP supports negative precision for its round function:

$a = round($a, -1);

it's pretty much the same as writing:

$a = round($a/10)*10;
like image 184
knittl Avatar answered Dec 24 '22 19:12

knittl


You've got to move your decimals over some, then ceil-ify.

ceil($a/10)*10
like image 43
Ben Avatar answered Dec 24 '22 17:12

Ben