简体   繁体   中英

How can I stop Java from shortening large double?

Let's suppose we have the following code:

System.out.println(String.valueOf(100000000000.0));

Now the output to that is 1.0E11. But that is not what I want. (Looks bad on a highscore) I want it to output exactly 100000000000.0. Is there a way to do that?

Format it appropriately. For example:

System.out.printf("%.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.

Or you could use String.format():

System.out.println(String.format("%.0f", 1654621658874684.0d));

System.out.printf("Score: %.0f\\n", 1e5); will print 100000 .

Refer to this ... Quest
You can use DecimalFormat to format your value for displaying

对于那些大数字,我认为你应该使用BigDecimal。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM