<?php
function my_round($value, $len = 0)
{
bcscale(18);
// 转化成高精度
$value = bcsub($value, 0);
// 乘以幂指数
$value_power = bcmul($value, bcpow(10, $len));
// 判断正负号
$flag = bccomp($value_power, 0);
// 根据正负,确定要比较的数
$cmp = $flag >= 0 ? 0.5 : -0.5;
// 整数部分
$value_power_int = bcsub($value_power, 0, 0);
// 小数部分
$diff = bccomp(
bcsub($value_power, $value_power_int),
$cmp
);
// 正数 大于0.5 入
if ($flag > 0 && $diff > 0) {
// +入
$value_power_int = bcadd($value_power_int, 1);
} elseif ($flag < 0 && $diff < 0) {
// 负数 小于-0.5 入
// -入
$value_power_int = bcadd($value_power_int, -1);
} elseif (!$diff) {
// 5留双
$mod = bcmod($value_power_int, 2, 0);
if ($mod == 1) {
// 正数留双
$value_power_int = bcadd($value_power_int, 1);
} elseif ($mod == -1) {
// 负数留双
$value_power_int = bcadd($value_power_int, -1);
}
} else {
//舍
}
return bcdiv($value_power_int, bcpow(10, $len), $len);
}