【问题标题】:convert a scientific notation/exponential number [duplicate]转换科学计数法/指数数[重复]
【发布时间】:2017-12-19 18:21:06
【问题描述】:

我正在尝试转换科学计数法/指数。

例如:

我从 API 请求中得到这个数字:4.96E-6

我想把它转换成他的正常值0.00000496。

我在网上搜索并尝试了一些代码,例如

$awb = "4.96e-6"; // tried with and without quotes
$format = sprintf($awb, "%e");
var_dump($format);

它返回:字符串(7)“4.96E-6”

$awb = 4.96e-6;
$format = sscanf($awb, "%e");
var_dump($format);

正在返回:

array(1) { [0]=> float(4.96E-6) }

请问我该怎么办?

谢谢

【问题讨论】:

  • 是496还是596?
  • @PatrickQ 虽然我确信这是重复的东西,但接受的答案是糟糕
  • 您要将其转换为不同格式的 string 用于输出,还是转换为 number 用于计算?
  • @Sammitch 是的,在我选择了那个之后,我发现了一个可能更好的 (stackoverflow.com/questions/5340283/…)。但不能撤回然后再次投票,所以我想我会按照原来的投票结果进行投票。

标签: php


【解决方案1】:

您可以使用 number_format() 函数;例如

<?php
$awb = "4.96e-6";
print number_format($awb, 10);
?>

将导致输出:0.0000049600。

见:http://php.net/manual/en/function.number-format.php

【讨论】:

    猜你喜欢
    • 2019-07-09
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 2011-11-19
    • 2012-06-22
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多