将包含科学记数的数字的字符串转换为PHP中的double

时间:2021-01-03 22:33:57

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

我需要帮助将包含科学记数的数字的字符串转换为double。

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

示例字符串:“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?

我正在考虑将数字分成左边的数字和指数而不只是做数学来生成数字;但有没有更好/标准的方法来做到这一点?

7 个解决方案

#1


10  

PHP is typeless dynamically typed, meaning it has to parse values to determine their types (recent versions of PHP have type declarations).

PHP是无类型的动态类型,这意味着它必须解析值以确定它们的类型(PHP的最新版本具有类型声明)。

In your case, you may simply perform a numerical operation to force PHP to consider the values as numbers (and it understands the scientific notation x.yE-z).

在您的情况下,您可以简单地执行数值运算以强制PHP将值视为数字(并且它理解科学记数法x.yE-z)。

Try for instance

试试吧

  foreach (array("1.8281e-009","2.3562e-007","0.911348") as $a)
  {
    echo "String $a: Number: " . ($a + 1) . "\n";
  }

just adding 1 (you could also subtract zero) will make the strings become numbers, with the right amount of decimals.

只需加1(你也可以减零)将使字符串成为数字,具有正确的小数位数。

Result:

  String 1.8281e-009: Number: 1.0000000018281
  String 2.3562e-007: Number: 1.00000023562
  String 0.911348:    Number: 1.911348

You might also cast the result using (float)

您也可以使用(float)转换结果

  $real = (float) "3.141592e-007";

#2


9  

$f = (float) "1.8281e-009";
var_dump($f); // float(1.8281E-9)

#3


3  

$float = sprintf('%f', $scientific_notation);
$integer = sprintf('%d', $scientific_notation);
if ($float == $integer)
{
    // this is a whole number, so remove all decimals
    $output = $integer;
}
else
{
    // remove trailing zeroes from the decimal portion
    $output = rtrim($float,'0');
    $output = rtrim($output,'.');
}

#4


3  

I found a post that used number_format to convert the value from a float scientific notation number to a non-scientific notation number:

我发现一个帖子使用number_format将值从浮点科学记数转换为非科学记数:

http://jetlogs.org/2008/02/05/php-problems-with-big-integers-and-scientific-notation/

Editor's note: Link is rotten

编者注:链接烂了

Example from the post:

帖子的例子:

$big_integer = 1202400000; 
$formatted_int = number_format($big_integer, 0, '.', ''); 
echo $formatted_int; //outputs 1202400000 as expected 

HTH

#5


2  

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

以下代码行可以帮助您显示bigint值,

$token=  sprintf("%.0f",$appdata['access_token'] );

refer with this link.

请参考此链接。

#6


0  

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

同时使用number_format()和rtrim()函数。例如

//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, '.');
}

#7


0  

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

使用0.000077240388

#1


10  

PHP is typeless dynamically typed, meaning it has to parse values to determine their types (recent versions of PHP have type declarations).

PHP是无类型的动态类型,这意味着它必须解析值以确定它们的类型(PHP的最新版本具有类型声明)。

In your case, you may simply perform a numerical operation to force PHP to consider the values as numbers (and it understands the scientific notation x.yE-z).

在您的情况下,您可以简单地执行数值运算以强制PHP将值视为数字(并且它理解科学记数法x.yE-z)。

Try for instance

试试吧

  foreach (array("1.8281e-009","2.3562e-007","0.911348") as $a)
  {
    echo "String $a: Number: " . ($a + 1) . "\n";
  }

just adding 1 (you could also subtract zero) will make the strings become numbers, with the right amount of decimals.

只需加1(你也可以减零)将使字符串成为数字,具有正确的小数位数。

Result:

  String 1.8281e-009: Number: 1.0000000018281
  String 2.3562e-007: Number: 1.00000023562
  String 0.911348:    Number: 1.911348

You might also cast the result using (float)

您也可以使用(float)转换结果

  $real = (float) "3.141592e-007";

#2


9  

$f = (float) "1.8281e-009";
var_dump($f); // float(1.8281E-9)

#3


3  

$float = sprintf('%f', $scientific_notation);
$integer = sprintf('%d', $scientific_notation);
if ($float == $integer)
{
    // this is a whole number, so remove all decimals
    $output = $integer;
}
else
{
    // remove trailing zeroes from the decimal portion
    $output = rtrim($float,'0');
    $output = rtrim($output,'.');
}

#4


3  

I found a post that used number_format to convert the value from a float scientific notation number to a non-scientific notation number:

我发现一个帖子使用number_format将值从浮点科学记数转换为非科学记数:

http://jetlogs.org/2008/02/05/php-problems-with-big-integers-and-scientific-notation/

Editor's note: Link is rotten

编者注:链接烂了

Example from the post:

帖子的例子:

$big_integer = 1202400000; 
$formatted_int = number_format($big_integer, 0, '.', ''); 
echo $formatted_int; //outputs 1202400000 as expected 

HTH

#5


2  

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

以下代码行可以帮助您显示bigint值,

$token=  sprintf("%.0f",$appdata['access_token'] );

refer with this link.

请参考此链接。

#6


0  

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

同时使用number_format()和rtrim()函数。例如

//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, '.');
}

#7


0  

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

使用0.000077240388