Convert a string containing a number in scientific

2020-02-17 09:46发布

I need help converting a string that contains a number in scientific notation to a double.

Example strings: "1.8281e-009" "2.3562e-007" "0.911348"

I was thinking about just breaking the number into the number on the left and the exponent and than just do the math to generate the number; but is there a better/standard way to do this?

8条回答
Luminary・发光体
2楼-- · 2020-02-17 10:48
function decimal_notation($float) {
        $parts = explode('E', $float);

        if(count($parts) === 2){
            $exp = abs(end($parts)) + strlen($parts[0]);
            $decimal = number_format($float, $exp);
            return rtrim($decimal, '.0');
        }
        else{
            return $float;
        }
    }

work with 0.000077240388

查看更多
Summer. ? 凉城
3楼-- · 2020-02-17 10:49

I tried the +1,-1,/1 solution but that was not sufficient without rounding the number afterwards using round($a,4) or similar

查看更多
登录 后发表回答