System.out.println(2.00-1.10); System.out.printf("%f",2.00-1.10);
解决方法
所以如果你不指定任何数字到%f,默认情况下它只打印1个字符.但是,如果要更改小数后的数字,则需要将其指定为%.2f,这将打印数字为2位小数.
所以它类似于写作
System.out.printf("%f",2.00-1.10);
要么
System.out.printf("%.1f",2.00-1.10);
由于float的格式说明符的一般语法是:
%[flags][width][.precision][argsize]typechar
在旁注:
Java中也有一个formatter class.
An interpreter for printf-style format strings. This class provides
support for layout justification and alignment,common formats for
numeric,string,and date/time data,and locale-specific output.
Common Java types such as byte,BigDecimal,and Calendar are
supported. Limited formatting customization for arbitrary user types
is provided through the Formattable interface.
If the precision is not specified then the default value is 6. If the precision is less than the number of digits which would appear after the decimal point in the string returned by Float.toString(float) or Double.toString(double) respectively,then the value will be rounded using the round half up algorithm.