Java 强制一个double来写整数
如何强制Java中的Java 强制一个double来写整数,java,double,Java,Double,如何强制Java中的double打印整数。不是像1.65462165887E12,而是像1654621658874684 谢谢您可以使用: String formatted=String.format(“%f”,dblNumber)将其适当格式化。例如: System.out.printf("%.1f", 1654621658874684.0); 也可以像字符串一样使用它: //"%.1f" this mean, how many number after the comma String v
double
打印整数。不是像1.65462165887E12,而是像1654621658874684
谢谢您可以使用:
String formatted=String.format(“%f”,dblNumber)代码>将其适当格式化。例如:
System.out.printf("%.1f", 1654621658874684.0);
也可以像字符串一样使用它:
//"%.1f" this mean, how many number after the comma
String value = String.format("%.1f", 1654621658874684.0);
请注意,double
并不是无限精确的。它的精度约为。如果您需要具有任意精度的浮点数,请使用BigDecimal
而不是double
我使用类似
if((long) d == d)
System.out.println((long) d);
else
System.out.println(d);
long可以有18位数,而double
最多有15-16位数的准确度。感谢您的快速回复+1,因为您提到了double中缺乏准确度可能是问题的一部分。感谢BigDecimal的提示
if((long) d == d)
System.out.println((long) d);
else
System.out.println(d);