Convert a string containing a number in scientific notation to a double in PHP

前端 未结 8 1583
伪装坚强ぢ
伪装坚强ぢ 2020-12-25 08:18

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 wa

相关标签:
8条回答
  • 2020-12-25 08:56

    Following line of code can help you to display bigint value,

    $token=  sprintf("%.0f",$scienticNotationNum );
    

    refer with this link.

    0 讨论(0)
  • 2020-12-25 08:56

    Use number_format() and rtrim() functions together. Eg

    //eg $sciNotation = 2.3649E-8
    $number = number_format($sciNotation, 10); //Use $dec_point large enough
    echo rtrim($number, '0'); //Remove trailing zeros
    

    I created a function, with more functions (pun not intended)

    function decimalNotation($num){
        $parts = explode('E', $num);
        if(count($parts) != 2){
            return $num;
        }
        $exp = abs(end($parts)) + 3;
        $decimal = number_format($num, $exp);
        $decimal = rtrim($decimal, '0');
        return rtrim($decimal, '.');
    }
    
    0 讨论(0)
提交回复
热议问题