Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 强制一个double来写整数_Java_Double - Fatal编程技术网

Java 强制一个double来写整数

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

如何强制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 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);