Rounding to nearest fraction (half, quarter, etc.)

后端 未结 4 1412
北荒
北荒 2020-11-28 06:43

So, I need to create the following functions but my head can\'t think of any possibility in PHP without complicated math.

  • Round always up to t
4条回答
  •  无人及你
    2020-11-28 07:07

    According to the mround() function in Excel:

    function MRound($num,$parts) {
        $res = $num * $parts;
        $res = round($res);
        return $res /$parts;
    }
    echo MRound(-1.38,4);//gives -1.5
    echo MRound(-1.37,4);//gives -1.25
    echo MRound(1.38,4);//gives 1.5
    echo MRound(1.37,4);//gives 1.25
    

提交回复
热议问题