Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String format and locale problems - Android

Tags:

java

android

xml

When I perform a truncate using:

label.setText(String.format("%.2f", 1.2975118));
// 1,30

I get comma(,) instead of point(.) and this causes my program crash since I need to perform operation on float numbers.

How I can truncate a float and .setText with a point instead of comma?

like image 970
Singee Avatar asked Apr 13 '16 16:04

Singee


1 Answers

Please be careful as String.format depend on your current Local configuration, you may not get a dot as a separator.

Prefer using String.format(java.util.Locale.US,"%.2f", floatValue);

Locale independent :

double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
like image 119
USKMobility Avatar answered Oct 25 '22 20:10

USKMobility