【问题标题】:scientific notation to normal decimal output conversion科学计数法到普通十进制输出的转换
【发布时间】:2019-01-06 14:23:08
【问题描述】:

首先,为了找到完全相同的问题,我做了很多尝试,但都失败了。有类似的解决方案和看起来不错但不适合我的解决方案。

在 PHP 中,我想用echo 或类似的东西将数字转换(或正确打印)到页面。

输入的数字可能会这样变化:

100000000

10

0.1

0.0000000001

100000000.0000000001

它们是从 MySQL 数据库中检索的。字段格式为double

但是,当我尝试echo 这些数字时,小十进制数字会以科学计数法打印

1E-11

我发现了sprintfnumber_formatstringfication、make (double) 或 (string) 等。但它们有一些不需要的功能,如下所示:

  1. 四舍五入
  2. 冗余 0(零)拖尾:例如)0.1 到 0.10000

我只是想打印出那些数字原样

并且没有多余的过程。 (例如通过 number_format 转换为十进制格式,然后将其变为字符串,然后删除零尾)

我该怎么做?

【问题讨论】:

    标签: php number-formatting


    【解决方案1】:

    我自己找到了一种方法,我正在发布一个快速答案。

    function realval($v) {
        $f = (string)number_format($v, 10, '.', ''); // 10000.0000010000
        if(strpos($f, '.') !== false) { // if it's a decimal (if not, print as is)
            $f = rtrim($f, "0"); // 10000.000001
            $f = rtrim($f, "."); // to prevent 10000. like things. 
        }
        return $f; // string format
    }
    

    这可以满足我的需要。我想要一个更体面的方式来做到这一点,但还没有想出来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      相关资源
      最近更新 更多