diff --git a/application/app/controller/Base.php b/application/app/controller/Base.php index acb506a..dfe8c07 100644 --- a/application/app/controller/Base.php +++ b/application/app/controller/Base.php @@ -309,34 +309,16 @@ class Base extends Controller{ } // 对于任意浮点字符串的指定位四舍五入 - public function bcRoundCustom($number, $scale) { - // 确保scale是整数 - $scale = intval($scale); + public function roundToString($numberStr, $numDecimals) { + // 将字符串转换为浮点数 + $number = floatval($numberStr); - // 构建10的$scale次方字符串 - $factor = '1' . str_repeat('0', $scale); + // 四舍五入到指定的小数位数 + $roundedNumber = round($number, $numDecimals); - // 乘以10的$scale次方 - $product = bcmul($number, $factor, $scale + 1); - - // 构建0.5乘以10的$scale次方字符串,用于四舍五入 - $halfStep = '0.' . str_repeat('0', $scale - 1) . '5'; - - // 检查是否需要进位 - if (bccomp($product, $halfStep, $scale + 1) >= 0) { - // 需要进位,添加1 - $roundedProduct = bcadd($product, '1', 0); // 这里我们只需要整数结果来确定是否进位 - } else { - // 不需要进位,直接使用原乘积 - $roundedProduct = $product; - } - - // 截断或四舍五入到指定的小数位数 - // 注意:由于我们之前已经多算了一位精度,现在需要除以$factor并保留$scale位小数 - $result = bcdiv($roundedProduct, $factor, $scale); - - return $result; - } + // 将结果转换回字符串 + return strval($roundedNumber); + } public function postRequest($url, $data = [], $headers = []) { diff --git a/application/app/controller/Sportstesting.php b/application/app/controller/Sportstesting.php index 630b7ad..6db728a 100644 --- a/application/app/controller/Sportstesting.php +++ b/application/app/controller/Sportstesting.php @@ -1142,7 +1142,9 @@ class Sportstesting extends Base{ $data['score'] = $value[2]; // 计算比例后分值 $proportional_post_score = bcmul($data['total_score'],$data['proportion'],2); - $data['proportion_value'] = bcmul($data['score'],bcdiv($proportional_post_score,100,2),1); + $data['proportion_value'] = bcmul($data['score'],bcdiv($proportional_post_score,100,2),2); + // 四舍五入一下结果 + $data['proportion_value'] = $this->roundToString($data['proportion_value'],1); break; } }