8

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

5
  • 2
    remove the ',' !!I think you wanted to add '.' Commented Jun 25, 2013 at 7:09
  • should I replace it with "." ? Commented Jun 25, 2013 at 7:12
  • @ayilmaz Yes, that is correct Commented Jun 25, 2013 at 7:13
  • @ayilmaz yes .replace it .I haved added the solution as answer also . Commented Jun 25, 2013 at 7:17
  • 5
    Your locale is not set correctly to allow commas in floating point numbers. You need to use a locale which allows this. Commented Jun 25, 2013 at 7:19

8 Answers 8

15

You are using a comma when you should be using a period

float f1 = Float.parseFloat("1.9698");

That should work.

Sign up to request clarification or add additional context in comments.

Comments

5

You have added comma instead of '.'

Do like this.

float f1 = Float.parseFloat("1.9698");

Comments

3

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

Comments

3
Float number;
String str=e1.getText().toString();
number = Float.parseFloat(str);

Or In one line

float float_no = Float.parseFloat("3.1427");

Comments

2
Float total = Float.valueOf(0);
try
{
    total = Float.valueOf(str);
}
catch(NumberFormatException ex)
{
    DecimalFormat df = new DecimalFormat();
    Number n = null;
    try
    {
        n = df.parse(str);
    } 
    catch(ParseException ex2){ }
    if(n != null)
        total = n.floatValue();
}

3 Comments

Some comments would be good. The questioner is asking why his code does not work. It would be nice to answer that question, too.
because comma(,) is not converted into float so we use decimalformat
This would be the right answer. This problem is because there is no character internationalization.And I googled that turkey uses a semicolon for decimals. Should use ICU developer.android.com/guide/topics/resources/… Look at this developer.android.com/reference/android/icu/text/DecimalFormat
1

This is Type Conversion: type conversion required when we use different type of data in any variables.

    String str = "123.22";

    int i = Integer.parseInt(str);
    float f = Float.parseFloat(str);
    long l = Long.parseLong(str);
    double d= Double.parseDouble(str);

    str = String.valueOf(d);
    str = String.valueOf(i);
    str = String.valueOf(f);
    str = String.valueOf(l);

Also some times we need Type Casting: type casting required when we use same data but in different type. only you cast Big to Small Datatype.

    i = (int)f;
    i = (int)d;
    i = (int)l;

    f = (float)d;
    f = (float)l;

    l = (long)d;

Comments

1

used this float f1 = Float.parseFloat("1.9698"); or replace ,(comma) with .(dot) that is invalid form of flot number

Comments

1

In kotlin:

stringValue.toFloatOrNull() ?: 0F

Which will give NumberFormatException free conversion.

More precise:

private fun getFloat(stringValue: String) = stringValue.toFloatOrNull() ?: 0F

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.