为什么PHP用科学的符号来打印我的数字,当我指定它为。000021?

时间:2021-12-24 01:15:19

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

7 个解决方案

#1


34  

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

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

print number_format($var, 5);

Also check sprintf()

还要检查sprintf()

#2


21  

2.1E-5 is the 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()。

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

2.1 e-5编辑如果你不熟悉语法,你应该知道,简称2.1×纯。它是大多数编程语言用科学符号表示数字的方式。

#3


6  

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 .000021 instead 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(比如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.

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

#4


5  

Use number_format or sprintf if you want to see the number as you expect.

如果您想按预期看到数字,请使用number_format或sprintf。

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

#5


2  

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个小数,而右边没有额外的0(如number_format所做的,这可能会很烦人),请使用以下方法:

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

回声空白(空白(sprintf(%。8 f ',$ var),“0”),“。”);

#6


0  

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.

此外,不同的存储方法占用的内存也不同。但是,上面提供的解决方案应该是有效的。只要记住,四舍五入误差可能发生在非常大或很小的数字上。

#7


-3  

<?php
$var = .000021;
echo $var; // 2.1E-5
echo "\n";
$var = '.000021';
echo $var; // .000021

Define your number as a string to avoid auto-casting when used in string context (i.e. echo)

将数字定义为字符串,以避免在字符串上下文中使用时自动强制转换(例如echo)

#1


34  

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

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

print number_format($var, 5);

Also check sprintf()

还要检查sprintf()

#2


21  

2.1E-5 is the 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()。

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

2.1 e-5编辑如果你不熟悉语法,你应该知道,简称2.1×纯。它是大多数编程语言用科学符号表示数字的方式。

#3


6  

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 .000021 instead 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(比如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.

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

#4


5  

Use number_format or sprintf if you want to see the number as you expect.

如果您想按预期看到数字,请使用number_format或sprintf。

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

#5


2  

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个小数,而右边没有额外的0(如number_format所做的,这可能会很烦人),请使用以下方法:

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

回声空白(空白(sprintf(%。8 f ',$ var),“0”),“。”);

#6


0  

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.

此外,不同的存储方法占用的内存也不同。但是,上面提供的解决方案应该是有效的。只要记住,四舍五入误差可能发生在非常大或很小的数字上。

#7


-3  

<?php
$var = .000021;
echo $var; // 2.1E-5
echo "\n";
$var = '.000021';
echo $var; // .000021

Define your number as a string to avoid auto-casting when used in string context (i.e. echo)

将数字定义为字符串,以避免在字符串上下文中使用时自动强制转换(例如echo)