PHP 舍入小数 [英] PHP round decimals

查看:34
本文介绍了PHP 舍入小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以像这样使用 PHP 将小数四舍五入到最接近的 0.5:

Is it possible to round a decimals to the nearest .5 with PHP like this:

number | round
----------------
 1.29  |  1.50
 2.03  |  2.00
 1.43  |  1.50
 1.13  |  1.00
11.38  | 11.50

我尝试过:

$rnd= round($number,2);

但我得到的小数类似于上面数字"列中的小数.

but I get decimals like the one in the column "number" above.

推荐答案

function round_to_nearest_half($number) {
    return round($number * 2) / 2;
}

这篇关于PHP 舍入小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆