round 对浮点数进行四舍五入
发表日期:2021-07-01 08:56:08 | 来源: | | 浏览(1197) 分类:Math 函数
round
(PHP 4, PHP 5, PHP 7, PHP 8)
round — 对浮点数进行四舍五入
说明
$val, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): float
返回将 val 根据指定精度
precision(十进制小数点后数字的数目)进行四舍五入的结果。precision
也可以是负数或零(默认值)。
参数
-
val -
要处理的值。
-
precision -
可选的十进制小数点后数字的数目。
If the
precisionis positive,valis rounded toprecisionsignificant digits after the decimal point.If the
precisionis negative,valis rounded toprecisionsignificant digits before the decimal point, i.e. to the nearest multiple ofpow(10, -precision), e.g. for aprecisionof -1valis rounded to tens, for aprecisionof -2 to hundreds, etc. -
mode -
Use one of the following constants to specify the mode in which rounding occurs.
常量 说明 PHP_ROUND_HALF_UPRounds valaway from zero when it is half way there, making 1.5 into 2 and -1.5 into -2.PHP_ROUND_HALF_DOWNRounds valtowards zero when it is half way there, making 1.5 into 1 and -1.5 into -1.PHP_ROUND_HALF_EVENRounds valtowards the nearest even value when it is half way there, making both 1.5 and 2.5 into 2.PHP_ROUND_HALF_ODDRounds valtowards the nearest odd value when it is half way there, making 1.5 into 1 and 2.5 into 3.
返回值
The value rounded to the given precision as a float.
范例
示例 #1 round() 例子
<?php var_dump(round(3.4)); var_dump(round(3.5)); var_dump(round(3.6)); var_dump(round(3.6, 0)); var_dump(round(5.045, 2)); var_dump(round(5.055, 2)); var_dump(round(345, -2)); var_dump(round(345, -3)); var_dump(round(678, -2)); var_dump(round(678, -3)); ?>
以上例程会输出:
float(3) float(4) float(4) float(4) float(5.05) float(5.06) float(300) float(0) float(700) float(1000)
示例 #2 How precision affects a float
<?php $number = 135.79; var_dump(round($number, 3)); var_dump(round($number, 2)); var_dump(round($number, 1)); var_dump(round($number, 0)); var_dump(round($number, -1)); var_dump(round($number, -2)); var_dump(round($number, -3)); ?>
以上例程会输出:
float(135.79) float(135.79) float(135.8) float(136) float(140) float(100) float(0)
示例 #3 mode 例子
<?php echo 'Rounding modes with 9.5' . PHP_EOL; var_dump(round(9.5, 0, PHP_ROUND_HALF_UP)); var_dump(round(9.5, 0, PHP_ROUND_HALF_DOWN)); var_dump(round(9.5, 0, PHP_ROUND_HALF_EVEN)); var_dump(round(9.5, 0, PHP_ROUND_HALF_ODD)); echo PHP_EOL; echo 'Rounding modes with 8.5' . PHP_EOL; var_dump(round(8.5, 0, PHP_ROUND_HALF_UP)); var_dump(round(8.5, 0, PHP_ROUND_HALF_DOWN)); var_dump(round(8.5, 0, PHP_ROUND_HALF_EVEN)); var_dump(round(8.5, 0, PHP_ROUND_HALF_ODD)); ?>
以上例程会输出:
Rounding modes with 9.5 float(10) float(9) float(10) float(9) Rounding modes with 8.5 float(9) float(8) float(8) float(9)
示例 #4 mode with precision examples
<?php echo 'Using PHP_ROUND_HALF_UP with 1 decimal digit precision' . PHP_EOL; var_dump(round( 1.55, 1, PHP_ROUND_HALF_UP)); var_dump(round(-1.55, 1, PHP_ROUND_HALF_UP)); echo PHP_EOL; echo 'Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision' . PHP_EOL; var_dump(round( 1.55, 1, PHP_ROUND_HALF_DOWN)); var_dump(round(-1.55, 1, PHP_ROUND_HALF_DOWN)); echo PHP_EOL; echo 'Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision' . PHP_EOL; var_dump(round( 1.55, 1, PHP_ROUND_HALF_EVEN)); var_dump(round(-1.55, 1, PHP_ROUND_HALF_EVEN)); echo PHP_EOL; echo 'Using PHP_ROUND_HALF_ODD with 1 decimal digit precision' . PHP_EOL; var_dump(round( 1.55, 1, PHP_ROUND_HALF_ODD)); var_dump(round(-1.55, 1, PHP_ROUND_HALF_ODD)); ?>
以上例程会输出:
Using PHP_ROUND_HALF_UP with 1 decimal digit precision float(1.6) float(-1.6) Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision float(1.5) float(-1.5) Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision float(1.6) float(-1.6) Using PHP_ROUND_HALF_ODD with 1 decimal digit precision float(1.5) float(-1.5)
- PHP(0)
- PHP杂项(34)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- abs 绝对值(0)
- acos 反余弦(0)
- acosh 反双曲余弦(0)
- asin 反正弦(0)
- asinh 反双曲正弦(0)
- atan2 两个参数的反正切(0)
- atan 反正切(0)
- atanh 反双曲正切(0)
- base_convert 在任意进制之间转换数字(0)
- bindec 二进制转换为十进制(0)
- ceil 进一法取整(0)
- cos 余弦(0)
- cosh 双曲余弦(0)
- decbin 十进制转换为二进制(0)
- dechex 十进制转换为十六进制(0)
- decoct 十进制转换为八进制(0)
- deg2rad 将角度转换为弧度(0)
- exp 计算 e 的指数(0)
- expm1 返回 exp(number) - 1,甚至当 number 的值接近零也能计算出准确结果(0)
- fdiv Divides two numbers, according to IEEE 754(0)
- floor 舍去法取整(0)
- fmod 返回除法的浮点数余数(0)
- getrandmax 显示随机数最大的可能值(0)
- hexdec 十六进制转换为十进制(0)
- hypot 计算一直角三角形的斜边长度(0)
- intdiv 对除法结果取整(0)
- is_finite 判断是否为有限值(0)
- is_infinite 判断是否为无限值(0)
- is_nan 判断是否为合法数值(0)
- lcg_value 组合线性同余发生器(0)
- log10 以 10 为底的对数(0)
- log1p 返回 log(1 + number),甚至当 number 的值接近零也能计算出准确结果(0)
- log 自然对数(0)
- max 找出最大值(0)
- min 找出最小值(0)
- mt_getrandmax 显示随机数的最大可能值(0)
- mt_rand 生成更好的随机数(0)
- mt_srand 播下一个更好的随机数发生器种子(0)
- octdec 八进制转换为十进制(0)
- pi 得到圆周率值(0)
- pow 指数表达式(0)
- rad2deg 将弧度数转换为相应的角度数(0)
- rand 产生一个随机整数(0)
- round 对浮点数进行四舍五入(0)
- sin 正弦(0)
- sinh 双曲正弦(0)
- sqrt 平方根(0)
- srand 播下随机数发生器种子(0)
- tan 正切(0)
- tanh 双曲正切(0)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)
宁公网安备 64010402001209号