[英]What are the semantics of java's postfix increment operator on the left side of an assignment statement and why do they differ from the right side?
[英]In the Java class DecimalFormat, what do the hashtags at the left side of the decimal do?
我对这个问题的标题不太好,但是希望我可以在本文中进一步解释。
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));
}
}
不要介意评论。 在Gr 11 Comp Sci课堂上,我问老师小数点左侧( 第11行 )的主题标签是否对双精度数有任何影响,我们在课堂上进行了尝试,发现它没有改变System.out.println语句。
~/workspace/Java/ $ java Output
43.97
有人可以向我解释小数点左边的参数的用途吗? 有人编程它做某事,所以我很好奇。
就像其他人所说的#
格式化数字,但掉零。
在您的示例中,将.###
应用于像12.34
这样的双12.340
格式, 它将格式化为12.340
但由于它的置零值没有,因此不会。
当您在小数点前放置#
,例如将###.###
格式化为12.34
会显示012.340
但由于它掉零,所以显示为12.34
也会发生同样的情况。
因此,小数点前的#
实际上不会执行任何操作。
的小数点前一些有用的例子,所以你可以看到格式之前小数可以工作是0
哪些格式的一个数字,但不会降为零,也可以在使用的DecimalFormat。 将000.000
类的模式应用于12.34
得到012.340
:
double d = 12.34;
DecimalFormat df = new DecimalFormat("000.000");
System.out.print(df.format(d));
在DecimalFormat中定义了0
, #
等模式。
#
格式化一个数字,但减零。 由于您的数字在小数点后有两位数字,因此第三个格式字符将被忽略。 如果您的数字小数点后有四位数字,那么它们将被减少为三位有效数字。
根据DecimalFormat docs #
,用于有条件地显示数字。
Symbol Location Localized? Meaning
# Number Yes Digit, zero shows as absent
请注意,已使用的#
事项的数量:
DecimalFormat x = new DecimalFormat(".###");
System.out.println(x.format(43.9711111)); // 43.971
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.