1

I need to convert my CMS data (which are provided as Strings) to float value, but I am getting exception

NumberFormatException: invalid float value: "16.385837"

The code looks like:

Double.valueOf(myString.trim()).doubleValue();

I've also tried like this:

Double.parseDouble(myString).doubleValue();

but i'm getting the same message. Do you have any idea what is wrong ?!

botang
  • 89
  • 2
  • 7

5 Answers5

2
try {
  String s = "16.385837";
  Double d = Double.parseDouble(s); 
  System.out.println(d);// which will prints 16.385837
} catch (NumberFormatException e) {
  // p did not contain a valid double
}
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
  • 1
    no. it will print correct result. may be your `myString` has wrong value. once check that value by printing System.out.println(myString+" "+myString.length()); – Ram kiran Pachigolla Jan 28 '13 at 12:38
  • 1
    check the length of the string and string value. – Ram kiran Pachigolla Jan 28 '13 at 12:38
  • for all strings that are causing parse error, length is one more greater than real length visible on the console. May it mean that there is some extra code at the end of the string causing the problem ? – botang Jan 28 '13 at 12:42
  • yes. if there is space then it won't be considered for parsing. which results parse error.. – Ram kiran Pachigolla Jan 28 '13 at 12:44
  • it have to work. If it doesn't work then you messed up something probably with locale. Are you sure that string contains "16.385837" not for example "16,385837" (you use Polish locale)? See http://stackoverflow.com/a/4323628/1387438 – Marek R Jan 28 '13 at 12:44
  • i've checked - there is one extra char at the end of the string causing this problem. i'll try to substring wrong strings. – botang Jan 28 '13 at 12:50
1
String s = e1.getText().toString();
Float f= Float.parseFloat(s);

use this code this will helps you

put your value on place of s; then you can parse string to float

kamal
  • 290
  • 3
  • 20
  • There is no method getText() for String object, it is not a View subclass – botang Jan 28 '13 at 12:38
  • i specially add one line put your value on place of s – kamal Jan 28 '13 at 12:59
  • there was another problem, my string contained some extra char at the end of it, and it was causing the problem. I've surrounded this block of code with try/catch and in catch block I substring this value, cutting off the last character. It works fine but thanks for help! – botang Jan 28 '13 at 13:16
  • the right answer that helped me with that problem is on the top of this thread but I voted up for your answer too – botang Jan 29 '13 at 12:57
0

Try this,

 Double.parseDouble(String.valueOf("16.385837"));
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22
0

try

Double.parseDouble(myString) not .doubleValue();

Sagar G.
  • 504
  • 7
  • 15
0

Try this

try {
        Double d = Double.parseDouble(String.valueOf("16.385837")); 
        System.out.println(d);
     } catch (NumberFormatException e) {
         // Handle The Exception During  Parsing
     }
edwin
  • 7,985
  • 10
  • 51
  • 82