简体   繁体   English

使用 PHP,我如何将大型 integer 舍入到最接近的百万

[英]Using PHP, how would I round a large integer to the nearest million

I have a situation where I am calculating the number of ad impressions per month.我有一种情况,我正在计算每月的广告展示次数。 I found several examples rounding up but they don't work in my case.我发现了几个例子,但它们在我的情况下不起作用。 The sample data I'm working with shows 166,666 impressions per month (166,666 x 12) is being rounded to 2 million.我正在使用的示例数据显示每月 166,666 次展示 (166,666 x 12) 被四舍五入为 200 万。 When multiplying 166666 by 12 then using the ceil() or round() functions, I get 1999992. I found an example of rounding to the nearest million but it was done in C or C++.当将 166666 乘以 12 然后使用 ceil() 或 round() 函数时,我得到 1999992。我找到了一个四舍五入到最接近百万的示例,但它是在 C 或 C++ 中完成的。

I've tested this:我已经对此进行了测试:

<?php
//
$val = 166666 * 12;
//
echo ceil($val/1E6)*1E6 . "<br />"; // => 2000000
echo number_format(ceil($val/1E6)*1E6, 0) . "<br />"; // => 2,000,000
?>

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

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