public static void main(String[] args) { DecimalFormat df = new DecimalFormat("###.####"); float f = 20.0f; System.out.println("你不想要的:" + f); System.out.println("你想要的答案:" + df.format(f));
}
tring str = "0.000";
Pattern pa1 = Pattern.compile("^[0-9].[1-9]+$");
if(pa1.matcher(str).matches()){
System.out.println(str);
}else{
String s = str.substring(0, str.lastIndexOf("."));
System.out.println(s);
}
float f = 20.1f; int fi=(int)f; if(f==fi) System.out.println(fi); else System.out.println(f);
本文通过示例展示了如何使用Java中的DecimalFormat类来格式化浮点数,同时介绍了如何判断浮点数是否为整数值的方法。
1249

被折叠的 条评论
为什么被折叠?



