This question already has an answer here:
I live in Belgium. And generally, in mathematics, we write our decimals with a comma like this: 3,141592
And that is also the result when I format the float.
System.out.println(String.format("%f", 3.141592));
So, the .
is replaced by a ,
like so: 3,141592
. So always when I need a point instead I have to add something like this: String.format("%f", 3.14).replace(',','.');
So, the question is: is there a way to change the Locale which makes every formatter in Java use a point, instead of comma?
Thanks
System.out.println(Locale.getDefault());
prints
nl_BE
Try using
String.format(Locale.US, "%f", floatValue)
for just setting locale used during formatting.For formatting numbers, you really should not rely on
toString()
. UseString.format()
, the factory methods inNumberFormat
, orDecimalFormat
, which allow you to control how numbers are formatted (the first two by choosing a Locale) without relying on some global setting.A simple solution, but would be wide reaching across the entire Locale, would be to set the system Locale to US or UK. Example.
Since you have changed the question, then you simply specify the local with your print method.
It may be useful to look at http://download.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html which contains a number of options for precise output of strings using Locale where necessary
It is often useful to route input and output through bespoke routines. Here is one from the JAMA library (http://math.nist.gov/javanumerics/jama/)
By using this you can be sure that future problems and enhancements are likely to be addressed