Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Float Formatting depends on Locale [duplicate]

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 
like image 627
Martijn Courteaux Avatar asked Dec 29 '10 11:12

Martijn Courteaux


2 Answers

Try using String.format(Locale.US, "%f", floatValue) for just setting locale used during formatting.

like image 116
rodion Avatar answered Oct 09 '22 19:10

rodion


A simple solution, but would be wide reaching across the entire Locale, would be to set the system Locale to US or UK. Example.

Locale.setDefault(Locale.US); 

Since you have changed the question, then you simply specify the local with your print method.

System.out.println(String.format(Locale.US, "%f", 3.141592)); 
like image 41
Codemwnci Avatar answered Oct 09 '22 21:10

Codemwnci