系统的java输出。出来格式(“%6f%n”,123.0);不像预期的那样
我目前正在学习如何用java格式化,遇到了以下问题
我有
System.out.format("%6f%n", 123.0);
我希望结果是123.0
,左边有一个空格(即输出的最小长度为6)。然而,我得到的却是
123.000000 //with no blank space on the left
为什么会这样?为什么小数点后有六个0?有人能给我解释一下吗
你可以在下面搜索框中键入要查询的问题!
我目前正在学习如何用java格式化,遇到了以下问题
我有
System.out.format("%6f%n", 123.0);
我希望结果是123.0
,左边有一个空格(即输出的最小长度为6)。然而,我得到的却是
123.000000 //with no blank space on the left
为什么会这样?为什么小数点后有六个0?有人能给我解释一下吗
# 1 楼答案
您需要指定小数点后的位数。如果没有,则假定为6位数。所以你可以做:
从Javadoc开始: