Jonas Bartkowski
Jonas Bartkowski

Reputation: 387

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?

Upvotes: 3

Views: 1130

Answers (4)

wonhee
wonhee

Reputation: 1661

For those kind of big numbers, I think you should use BigDecimal.

Upvotes: 0

RamValli
RamValli

Reputation: 4485

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

Upvotes: 0

Ima Miri
Ima Miri

Reputation: 967

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));

Upvotes: 6

tkroman
tkroman

Reputation: 4798

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

Upvotes: 2

Related Questions