[英]In the Java class DecimalFormat, what do the hashtags at the left side of the decimal do?
My title of this question I have isn't so great, but hopefully I can explain it more in this post. 我对这个问题的标题不太好,但是希望我可以在本文中进一步解释。
import java.io.*;
import java.text.*;
public class Output {
public static void main(String[] args) {
/*double number = 438.978;
/*UpperCase <- naming convention for classes.DecimalFormat x = new DecimalFormat("#.#");
System.out.println(x.format(number));*/
double number = 43.97;
DecimalFormat x = new DecimalFormat(".###");
System.out.println(x.format(number));
}
}
Don't mind the comments. 不要介意评论。 During my Gr 11 Comp Sci class I asked my teacher if the hashtags on the left of the decimal point( 11th line ) did anything to the double number, we tried it as a class and we found that it did not change the output of the System.out.println statement.
在Gr 11 Comp Sci课堂上,我问老师小数点左侧( 第11行 )的主题标签是否对双精度数有任何影响,我们在课堂上进行了尝试,发现它没有改变System.out.println语句。
~/workspace/Java/ $ java Output
43.97
Can someone explain to me the purpose of the parameters to the left of the decimal? 有人可以向我解释小数点左边的参数的用途吗? Someone programmed it to do something so I was just curious.
有人编程它做某事,所以我很好奇。
Like others have said #
formats a digit but drops zeros. 就像其他人所说的
#
格式化数字,但掉零。
In your example where you apply .###
to a double like 12.34
it would format to 12.340
but since it drops zeros it does not. 在您的示例中,将
.###
应用于像12.34
这样的双12.340
格式, 它将格式化为12.340
但由于它的置零值没有,因此不会。
The same happens when you put a #
before the decimal, for example ###.###
to format 12.34
would display 012.340
but since it drops zeros it displays as 12.34
. 当您在小数点前放置
#
,例如将###.###
格式化为12.34
会显示012.340
但由于它掉零,所以显示为12.34
也会发生同样的情况。
So a #
before the decimal will really do nothing. 因此,小数点前的
#
实际上不会执行任何操作。
An example of something useful before the decimal, so you can see formats before the decimal can work is 0
which formats a digit but does not drop zeros, and can also be used in the DecimalFormat. 的小数点前一些有用的例子,所以你可以看到格式之前小数可以工作是
0
哪些格式的一个数字,但不会降为零,也可以在使用的DecimalFormat。 A pattern like 000.000
applied to 12.34
results in 012.340
: 将
000.000
类的模式应用于12.34
得到012.340
:
double d = 12.34;
DecimalFormat df = new DecimalFormat("000.000");
System.out.print(df.format(d));
Patterns like 0
, #
and more are defined in DecimalFormat . 在DecimalFormat中定义了
0
, #
等模式。
#
formats a digit, but drops zeros. #
格式化一个数字,但减零。 Since your number has two digits after the decimal, the third format character is ignored. 由于您的数字在小数点后有两位数字,因此第三个格式字符将被忽略。 If you had a number with four digits after the decimal point, they would have been reduced to three significant digits.
如果您的数字小数点后有四位数字,那么它们将被减少为三位有效数字。
As per DecimalFormat docs #
is used to conditionally show the digit. 根据DecimalFormat docs
#
,用于有条件地显示数字。
Symbol Location Localized? Meaning
# Number Yes Digit, zero shows as absent
Do note that the number of #
used matters: 请注意,已使用的
#
事项的数量:
DecimalFormat x = new DecimalFormat(".###");
System.out.println(x.format(43.9711111)); // 43.971
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.