I currently use the following code to print a double:
return String.format("%.2f", someDouble);
This works well, except that Java uses my Locale's decimal separator (a comma) while I would like to use a point. Is there an easy way to do this?
I currently use the following code to print a double:
return String.format("%.2f", someDouble);
This works well, except that Java uses my Locale's decimal separator (a comma) while I would like to use a point. Is there an easy way to do this?
Use the overload of
String.format
which lets you specify the locale:If you're only formatting a number - as you are here - then using
NumberFormat
would probably be more appropriate. But if you need the rest of the formatting capabilities ofString.format
, this should work fine.You can use NumberFormat and DecimalFormat.
Take a look at this link from Java Tutorials LocaleSpecific Formatting
The section titled Locale-Sensitive Formatting is what you need.
You can pass an additional Locale to java.lang.String.format as well as to java.io.PrintStream.printf (e.g. System.out.printf()):
This results in the following (on my PC):
See String.format in the Java API.
I had the same issue..
55.1
transformed to55,10
. My quick (dirty?) fix is :String.format("%.2f", value).replaceAll(",",".");
Way too late but as other mentioned here is sample usage of
NumberFormat
(and its subclassDecimalFormat
)A more drastic solution is to set your Locale early in the main().
Like: