只有当浮点数与零不同时,我才需要显示浮点数的小数位。
如何使用regex或DecimalFormat以这种方式格式化?
发布于 2016-07-17 10:09:36
若要以您希望的方式显示小数,请使用以下代码片段:
// This is to show symbol . instead of ,
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US);
// Define the maximum number of decimals (number of symbols #)
DecimalFormat df = new DecimalFormat("#.##########", otherSymbols);
// type: double
System.out.println(df.format(5.0)); // writes 5
System.out.println(df.format(7.3)); // writes 7.3
System.out.println(df.format(9.000003)); // writes 9.000003
// type: float
System.out.println(df.format(9.000003f)); // writes 9.000002861
https://stackoverflow.com/questions/38424291
复制相似问题