PHP舍入数字 [英] PHP Rounding Numbers

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

问题描述

我似乎无法弄清楚如何始终使用PHP进行取整. ceil()是显而易见的选择,但我需要与round()提供的第二个参数"precision"相同的功能.这是一个示例:

I cannot seem to figure out how to always round up in PHP. ceil() would be the obvious choice but I need the same functionality that round() provides with its second parameter "precision". Here is an example:


// Desired result: 6250
echo round(6244.64, -2); // returns 6200
echo round(6244.64, -1); // returns 6240
echo ceil(6244.64) // returns 6245

我需要数字始终四舍五入,以便我可以根据图表的参数计算相等的除数.

I need the number to always round up so that I can calculate an equal divisor based on parameters for a chart.

推荐答案

来自对 http://php.net/manual/zh/function.ceil.php :

Quick and dirty `ceil` type function with precision capability.

function ceiling($value, $precision = 0) {
    return ceil($value * pow(10, $precision)) / pow(10, $precision);
}

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

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