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

Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189

8 Answers8

14

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

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

That should work.

JREN
  • 3,572
  • 3
  • 27
  • 45
5

You have added comma instead of '.'

Do like this.

float f1 = Float.parseFloat("1.9698");
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
3
Float number;
String str=e1.getText().toString();
number = Float.parseFloat(str);

Or In one line

float float_no = Float.parseFloat("3.1427");
Dharman
  • 30,962
  • 25
  • 85
  • 135
Vaishali Sharma
  • 602
  • 3
  • 6
  • 24
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();
}
HebeleHododo
  • 3,620
  • 1
  • 29
  • 38
Makvin
  • 3,475
  • 27
  • 26
  • Some comments would be good. The questioner is asking why his code does not work. It would be nice to answer that question, too. – Werner Henze Nov 23 '16 at 06:05
  • because comma(,) is not converted into float so we use decimalformat – Makvin Nov 24 '16 at 04:50
  • 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 https://developer.android.com/guide/topics/resources/internationalization Look at this https://developer.android.com/reference/android/icu/text/DecimalFormat – likeme Aug 25 '22 at 06:21
2

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());
    }
}
bko
  • 96
  • 3
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;
Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
1

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

1

In kotlin:

stringValue.toFloatOrNull() ?: 0F

Which will give NumberFormatException free conversion.

More precise:

private fun getFloat(stringValue: String) = stringValue.toFloatOrNull() ?: 0F
vishnu benny
  • 998
  • 1
  • 11
  • 15