简体   繁体   English

如何使数字在php中四舍五入为100?

[英]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. 因此,(floor($ a / 100))* 100是您的窍门。 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 我建议你使用mod操作

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 同样,如果您想向下舍入到其他任何数字(例如10),请遵循以下代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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