PHP中根据分数四舍五入十进制数

编程入门 行业动态 更新时间:2024-10-18 08:33:05
本文介绍了PHP中根据分数四舍五入十进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在努力将 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中根据分数四舍五入十进制数

本文发布于:2023-11-10 02:43:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574119.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分数   四舍五入   十进制数   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!