My app is working on many devices without problems so far. But now I got my new Galaxy Tab with Android 3.2 where it crashes all the time. I found out that the problem was a float in an EditText.
I am using myEditText.setText(String.format("%.1f", fMyFloat)); to put the float in the EditText. But somehow the float on my 3.2 Galaxy Tab is generated with a comma instead of a point. When I read the EditText back the app crashes of course, telling me that this is no valid float because of the comma…
What is going wrong here?
Convert float to string..
From the documentation of
String.format:The quoted text above means that the output of
String.formatwill match the default locale the user uses.As an example a comma would be used as the decimal-point-delimiter if it’s a user using Swedish locale, but a dot if it’s using an American.
If you’d like to force what locale is going to be used, use the overload of
String.formatthat accepts three parameters:Convert string to float..
Parsing an arbitrary string into a float using the default locale is quite easy, all you need to do is to use
DecimalFormat.parse.Then use
.parseto get aNumberand callfloatValueon this returned object.