Reputation: 14050
This code:
System.out.println(String.format("%f", Math.PI));
System.out.println(Math.PI);
produces that result on my computer:
3,141593
3.141592653589793
Why does the first line print float number with comma while the second one uses dot?
Upvotes: 2
Views: 2531
Reputation: 23655
String.format() uses the locale of your system. There should also be a variant of that method, you can pass a specific Locale instance. When using println() on the other hand, the object's toString() method is invoked - in your case Double#toString(), which is mosten times a rather basic implementation, just good enough to present a understandable display of of the object.
Upvotes: 0
Reputation: 104178
The first one uses the default locale.
The second one probably uses a default (not localizable) implementation.
Upvotes: 0
Reputation: 7061
String.format(String, Object...)
uses the formatting rules from Locale.getDefault()
. String.valueOf(double)
does not. Use String.format(Locale, String, Object...)
to explicitly specify the locale to use.
Upvotes: 4
Reputation: 54005
The former respects your locale settings (which seems to use comma for thousand separator).
Upvotes: 2