PHP 科学计数法转小数正常数值
/** * @param $num 科学计数法字符串 * @param int $double 小数点保留位数 默认10位 * @return string */ function sctonum($num, $double = 10){ if(stripos($num, "e") !== false){ $a = explode('e', strtolower($num)); $str = bcmul($a[0], bcpow(10, $a[1], $double), $double); $num = rtrim(rtrim($str, '0'), '.'); //去除小数后多余的0 } return $num; }
使用示例:
$obj = '{"id":130126199,"str":0.0000852}'; $arr = json_decode($obj, TRUE); foreach ($arr as $key => $val) { // $arr[$key] = number_format($val, 0, '', ''); $arr[$key] = $this->sctonum($val); } var_dump($arr); //输出 array(2) { ["id"]=> int(130126199) ["str"]=> string(9) "0.0000852" }