为什么 PHP 用科学记数法打印我的数字,当我将它指定为 .000021 时?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1471674/
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 02:44:37  来源:igfitidea点击:

Why is PHP printing my number in scientific notation, when I specified it as .000021?

phpsyntaxformattingnumbers

提问by ahmed

In PHP I have the following code:

在 PHP 中,我有以下代码:

<?PHP
  $var = .000021;
  echo $var;
?>

the output is 2.1E-5 !

输出为 2.1E-5 !

Why? it should print .000021

为什么?它应该打印 .000021

回答by acrosman

Use number_format()to get what you're after:

使用number_format()来获取您想要的内容:

print number_format($var, 5);

Also check sprintf()

还要检查sprintf()

回答by Kip

2.1E-5 isthe same number as 0.000021. That's how it prints numbers below 0.001. Use printf()if you want it in a particular format.

2.1E-5相同的数量0.000021。这就是它打印低于 0.001 的数字的方式。如果您需要特定格式,请使用printf()

EditIf you're not familiar with the 2.1E-5syntax, you should know it is shorthand for 2.1×10-5. It is how most programming languages represent numbers in scientific notation.

编辑如果您不熟悉2.1E-5语法,您应该知道它是 2.1×10 -5 的简写。这是大多数编程语言以科学记数法表示数字的方式。

回答by Vex

Use number_formator sprintfif you want to see the number as you expect.

如果您想按预期查看数字,请使用number_formatsprintf

echo sprintf('%f', $var);
echo number_format($var, 6);

回答by bluebrother

In general, a number is a number, not a string, and this means that any programming language treats a number as a number. Thus, the number by itself doesn't imply any specific format (like using .000021instead of 2.1e-5). This is nothing different to displaying a number with leading zeros (like 0.000021) or aligning lists of numbers. This is a general issue you'll find in any programming language: if you want a specific format you need to specify it, using the format functions of your programming language.

通常,数字是数字,而不是字符串,这意味着任何编程语言都将数字视为数字。因此,数字本身并不暗示任何特定格式(例如使用.000021代替2.1e-5)。这与显示带有前导零0.000021的数字(如)或对齐数字列表没有什么不同。这是您在任何编程语言中都会发现的一个普遍问题:如果您想要一个特定的格式,您需要使用您的编程语言的格式函数来指定它。

Unless you specify the number as string and convert it to a real number when needed, of course. Some languages can do this implicitly.

当然,除非您将数字指定为字符串并在需要时将其转换为实数。有些语言可以隐式地做到这一点。

回答by Joey Rich

To show a number up to 8 decimal spaces, without extra zeroes to the right (as number_format does, which can be annoying), use this:

要显示最多 8 个小数位的数字,并且右边没有多余的零(就像 number_format 那样,这可能很烦人),请使用以下命令:

echo rtrim(rtrim(sprintf('%.8F', $var), '0'), ".");

回答by Student101

Programming languages have different methods for storing numbers in memory. This is determined by the type of number that is being used. In your case, you have a floating point number (a fraction) that is to large to be stored as a fixed point number ( fractions are stored in this manner depending on their size).

编程语言有不同的方法在内存中存储数字。这是由正在使用的号码类型决定的。在您的情况下,您有一个浮点数(一个小数),它太大而无法存储为定点数(小数以这种方式存储,具体取决于它们的大小)。

This is a very important feature especially when working with very large or very small numbers. For instance, NASA or spaceX uses special storage methods for its calculations to ensure that the rockets the re-enter earths orbit land where they should.

这是一个非常重要的特性,尤其是在处理非常大或非常小的数字时。例如,NASA 或 spaceX 使用特殊的存储方法进行计算,以确保重新进入地球轨道的火箭降落在应有的位置。

Also, different storage methods take up different amounts of memory. However, the solution provided above should work. Just remember round off errors might occur with very big or small numbers.

此外,不同的存储方法占用不同的内存量。但是,上面提供的解决方案应该有效。请记住,非常大或非常小的数字可能会出现舍入错误。

回答by Binar Web

The previous answers responded to OP question, but none offered the code to do it.
Use this function to format any number with E-format.

以前的答案回答了 OP 问题,但没有人提供代码来做到这一点。
使用此功能可将任何数字格式化为E格式。

function format_amount_with_no_e($amount) {
    $amount = (string)$amount; // cast the number in string
    $pos = stripos($amount, 'E-'); // get the E- position
    $there_is_e = $pos !== false; // E- is found

    if ($there_is_e) {
        $decimals = intval(substr($amount, $pos + 2, strlen($amount))); // extract the decimals
        $amount = number_format($amount, $decimals, '.', ','); // format the number without E-
    }

    return $amount;
}

Please note the function will always return a string.

请注意该函数将始终返回一个字符串。