Singee
Singee

Reputation: 523

String format and locale problems - Android

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?

Upvotes: 7

Views: 8876

Answers (1)

Umesh Singh Kushwaha
Umesh Singh Kushwaha

Reputation: 5741

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));

Upvotes: 13

Related Questions