Force a double to write the whole Number

2019-05-08 21:26发布

How can I force a double in Java to print the whole number. Not like 1.65462165887E12.But like 1654621658874684?

Thanks

标签: java double
4条回答
聊天终结者
2楼-- · 2019-05-08 21:48

You could use String.format():

System.out.println(String.format("%.0f", 1654621658874684.0d)); // prints 1654621658874684
查看更多
家丑人穷心不美
3楼-- · 2019-05-08 22:02

String formatted = String.format("%f", dblNumber);

查看更多
够拽才男人
4楼-- · 2019-05-08 22:03

Format it appropriately. For example:

System.out.printf("%.1f", 1654621658874684.0);

Or you can use it like a String:

//"%.1f" this mean, how many number after the comma
String value = String.format("%.1f", 1654621658874684.0);

Be aware that double is not infinitely precise. It has a precision of about 15 to 17 decimal digits. If you need floating-point numbers with arbitrary precision, use BigDecimal instead of double.

查看更多
戒情不戒烟
5楼-- · 2019-05-08 22:03

I use something like

if((long) d == d)
  System.out.println((long) d);
else
  System.out.println(d);

long can have 18 digits whereas double has 15-16 digits of accuracy at best.

查看更多
登录 后发表回答