将包含科学记数法数字的字符串转换为 PHP 中的双精度数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4576927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:32:11  来源:igfitidea点击:

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

phpstringdoublescientific-notation

提问by Bilal Shahid

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"

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

我正在考虑将数字分解为左侧的数字和指数,而不仅仅是进行数学运算来生成数字;但是有没有更好/标准的方法来做到这一点?

采纳答案by e2-e4

PHP is typelessdynamically 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";

回答by Matthew

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

回答by Luca Borrione

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

回答by ShivarajRH

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

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

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

refer with this link.

请参阅此链接

回答by Jeff V

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/

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

HTH

回答by aphoe

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

回答by Falselight

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

回答by David Robertson

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

我尝试了 +1,-1,/1 解决方案,但如果不使用 round($a,4) 或类似方法对数字进行四舍五入,那还不够