[FIXED] convert string to float in android

Issue

I am trying to convert string to float but I couldnt succeed.

here is my code

float f1 = Float.parseFloat("1,9698");

the error it gives is

Invalid float  "1,9698";

why is it doing this? it is a valid float

Solution

I suggest you use something like this:

String text = ...;
text = text.replace(",", ".");

if(text.length() > 0) {
    try {
        float f = Float.parseFloat(text);
        Log.i(TAG, "Found float " + String.format(Locale.getDefault(), "%f", f));
    } catch (Exception e) {
        Log.i(TAG, "Exception " + e.toString());
    }
}

Answered By – bko

Answer Checked By – Dawn Plyler (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published