校验数值
This commit is contained in:
@@ -4,6 +4,7 @@ namespace app\api\model;
|
||||
|
||||
use think\Cache;
|
||||
use think\Db;
|
||||
use think\Log;
|
||||
use think\Model;
|
||||
use Yzh\YunPay;
|
||||
|
||||
@@ -318,6 +319,8 @@ class UserWallet extends Model
|
||||
$user_earnings = $user_wallet['earnings'];
|
||||
$original_coin = $user_earnings; // 保存原始值用于日志
|
||||
|
||||
$money = $this ->formatScientificNumber($money);
|
||||
// Log::record("计算收益:".$money,"info");
|
||||
if(in_array($change_type,$in_types)){
|
||||
$update_coin = bcadd($user_earnings , $money,4);
|
||||
}elseif(in_array($change_type,$out_types)){
|
||||
@@ -352,4 +355,29 @@ class UserWallet extends Model
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
function formatScientificNumber($num) {
|
||||
// 1. 先将数值转为字符串,方便正则匹配
|
||||
$numStr = (string)$num;
|
||||
|
||||
// 2. 正则匹配科学计数法格式(匹配 E/e 开头的科学计数法,包含正负指数)
|
||||
$scientificPattern = '/^[+-]?\d+(\.\d+)?[Ee][+-]?\d+$/';
|
||||
|
||||
// 3. 判断是否是科学计数法
|
||||
if (preg_match($scientificPattern, $numStr)) {
|
||||
// 是科学计数法,转换为浮点数后保留4位小数
|
||||
$floatNum = (float)$numStr;
|
||||
// 保留4位小数,四舍五入,确保格式正确
|
||||
$result = number_format($floatNum, 4, '.', '');
|
||||
// echo "检测到科学计数法数值,处理后结果:{$result}\n";
|
||||
} else {
|
||||
// 不是科学计数法,直接保留4位小数(兼容普通数值)
|
||||
$floatNum = (float)$numStr;
|
||||
$result = number_format($floatNum, 4, '.', '');
|
||||
// echo "非科学计数法数值,处理后结果:{$result}\n";
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user