优化估分计算得分值

This commit is contained in:
tiansf 2024-08-08 01:00:26 +08:00
parent 568cafb817
commit 7431951c89
2 changed files with 11 additions and 27 deletions

View File

@ -309,33 +309,15 @@ class Base extends Controller{
} }
// 对于任意浮点字符串的指定位四舍五入 // 对于任意浮点字符串的指定位四舍五入
public function bcRoundCustom($number, $scale) { public function roundToString($numberStr, $numDecimals) {
// 确保scale是整 // 将字符串转换为浮点
$scale = intval($scale); $number = floatval($numberStr);
// 构建10的$scale次方字符串 // 四舍五入到指定的小数位数
$factor = '1' . str_repeat('0', $scale); $roundedNumber = round($number, $numDecimals);
// 乘以10的$scale次方 // 将结果转换回字符串
$product = bcmul($number, $factor, $scale + 1); return strval($roundedNumber);
// 构建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;
} }

View File

@ -1142,7 +1142,9 @@ class Sportstesting extends Base{
$data['score'] = $value[2]; $data['score'] = $value[2];
// 计算比例后分值 // 计算比例后分值
$proportional_post_score = bcmul($data['total_score'],$data['proportion'],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; break;
} }
} }