简体   繁体   中英

PHP How to round numbers up correctly?

I have a loop which loops items by 24 per time. So it loops 24 items per page. In a page where there are 52 items it does not show the number 3 when I divide 24 by 52.

Example

$TotalProductsString=52;
$theLimitOfItems=12;
$TotalPageSum=$TotalProductsString/$theLimitOfItems;
echo round($TotalPageSum, 2); // display 2.17
echo round($TotalPageSum, 2); // display 2.2
echo round($TotalPageSum, 0.60); // display 2
echo round($TotalPageSum, 0.50); // display 2

It should display 3 as there is a remainder 52/24 = 2.16 with remainder 4 (24+24=48 + 4 = 52)

Try Ceil()

echo ceil(4.3);    // 5
echo ceil(9.999);  // 10
echo ceil(-3.14);  // -3

From php.net

echo ceil($TotalPageSum); // Rounds the fraction up

此处阅读有关PHP手册中的ceil()函数的信息

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM