简体   繁体   中英

How to make number to round down to divisible by 100 in php?

我只想将数字四舍五入为100,即如果number = 299,则240我想输出200。

<?php

$a = 240; 

$b = 260; 

echo (floor($a / 100))*100;

echo "<br>";

echo (floor($b / 100))*100;

So, (floor( $a / 100 ))*100 is the trick for you here. Cheers :)

Using division will give you floating point number, which will again need round function to convert it into integer. I would suggest you to use mod operation

eg.

$num1 = 278;
$num2 = 100;  // or it could be 10,100,1000

$result = $num1 - ($num1 % $num2);
//e.g. 200 = 278 - (278%100)

Here is the answer that can help you to round down the number to nearest

    $number = 299;
    echo '<br/>Result '.floor($number / 100) * 100;

Similarly if you want to round down to any other figure like 10 then follow below code

$number = 90;
    echo '<br/>Result '.floor($number / 10) * 10;

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