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

发布于 2024-10-10 03:44:50 字数 152 浏览 2 评论 0 原文

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

示例字符串: “1.8281e-009” “2.3562e-007” “0.911348”

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

萌︼了一个春 2024-10-17 03:44:50

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

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

例如,尝试

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

仅加 1(也可以减零)将使字符串变成数字,并具有适当的小数位数。

结果:

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

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

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

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

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).

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.

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)

  $real = (float) "3.141592e-007";
梦亿 2024-10-17 03:44:50
$f = (float) "1.8281e-009";
var_dump($f); // float(1.8281E-9)
$f = (float) "1.8281e-009";
var_dump($f); // float(1.8281E-9)
荒路情人 2024-10-17 03:44:50

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

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

请参考此链接

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

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

refer with this link.

恏ㄋ傷疤忘ㄋ疼 2024-10-17 03:44:50
$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,'.');
}
$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,'.');
}
咋地 2024-10-17 03:44:50

一起使用 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

我创建了一个函数,具有更多函数(不是双关语)

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

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, '.');
}
静若繁花 2024-10-17 03:44:50

我发现一篇文章,使用 number_format 将值从浮点科学记数法数字转换为非科学记数法数字:

帖子中的示例:

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

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

Example from the post:

$big_integer = 1202400000; 
$formatted_int = number_format($big_integer, 0, '.', ''); 
echo $formatted_int; //outputs 1202400000 as expected 
话少心凉 2024-10-17 03:44:50
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;
        }
    }

使用 0.000077240388

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

天冷不及心凉 2024-10-17 03:44:50

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文