3

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?

Jonas Bartkowski
  • 357
  • 1
  • 6
  • 15
  • @HotLicks Would be better if you have posted an answer showing how – Bren Feb 22 '14 at 13:41
  • Are you sure your high scores should be doubles in the first place? Even if fractional scores are allowed, I would be rather surprised to find that scoring 0.1 points and then 0.2 points was very slightly different from scoring 0.3 points. – user2357112 Feb 22 '14 at 13:48

4 Answers4

6

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));
Ima Vafaei
  • 927
  • 1
  • 14
  • 32
2

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

tkroman
  • 4,811
  • 1
  • 26
  • 46
0

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

Community
  • 1
  • 1
RamValli
  • 4,389
  • 2
  • 33
  • 45
0

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

wonhee
  • 1,581
  • 1
  • 14
  • 24