我正在努力将 excel 公式转换为 php 函数.现在我面临一个作为分数四舍五入的问题.请让我解释一下:
I am working to convert an excel formula to php function. Now I am facing a problem that is round up as the fraction. Please let me explain it:
- (2 * 18)/2.98 = 12.08 但根据我的要求应该是 12.25
- (2 * 18.5)/2.98 = 12.416 但根据我的要求应该是 12.50
- (2 * 18.8)/2.98 = 12.617 但根据我的要求应该是 12.75
在excel中,它是这样做的:
In excel, it is done like :
- 天花板(( 2 * 18 )/2.98, 0.25) = 12.25
- 天花板(( 2 * 18.5 )/2.98, 0.25) = 12.50
- 天花板(( 2 * 18.8 )/2.98, 0.25) = 12.75
我在 PHP 中尝试过.Ceil() 和 Floor() 没有达到目的,因为这两个将相应地给出输出 13 和 12.
I tried in PHP. Ceil() and Floor() do not serve the purpose as these 2 shall give output 13 and 12 accordingly.
另一轮() 使用了这些选项PHP_ROUND_HALF_UP、PHP_ROUND_HALF_DOWN、PHP_ROUND_HALF_EVEN 或 PHP_ROUND_HALF_ODD,这些选项没有达到目的.
Another one round() uses these options PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN, or PHP_ROUND_HALF_ODD which does not serve the purpose.
有人帮我吗?
推荐答案使用这个功能:
function fractionRound($num,$frac) { return ceil($num/$frac)*$frac; }这样称呼:
echo fractionRound(( 2 * 18 )/2.98, 0.25);它将四舍五入到最接近的 0.25 它将返回 12.25
it will round to the nearest 0.25 it will return 12.25
更多推荐
PHP中根据分数四舍五入十进制数
发布评论