[英]Round down to nearest multiple of 10000 using PHP
If I have the following code which grabs an array
of values and adds them all together, how can I then round them down to the nearest 10000 using PHP? 如果我有下面的代码,抓住一个
array
值,并增加了它们放在一起,我怎么能那么圆他们使用PHP到最近的 10000?
Here's the code I currently have 这是我目前的代码
$rows = $db->get("sales");
$sales = 0;
foreach($rows as $row) {
$stock = $sales + $row['sales'];
}
return $sales;
An example result would be 一个示例结果将是
146740
How could I then make that returned as 我怎么能把它作为返回
140000
Although if I had a number greater than 1 million , how could I have that returned as just 1 million ? 虽然如果我有一个超过100万的数字 ,我怎么能只返回100万 ?
Divide by 10000, use floor
to round down to an integer, then multiply by 10000: 除以10000,使用
floor
舍入为整数,然后乘以10000:
$x = 146740;
$x = 10000 * floor($x/10000);
Or subtract the remainer: 或减去剩余物:
$x = 146740;
$x = $x - ($x % 10000);
To extend this to 1 million, you can do: 要将此扩展到100万,您可以:
if ($x > 1000000) {
$divisor = 1000000;
} elseif ($x > 10000) {
$divisor = 10000;
} else {
$divisor = 1;
}
$x = $x - ($x % divisor);
你可以将值除以1000.如果它是整数146740/1000 = 146.然后乘以1000将得到146000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.