Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to Integer/Float/Double

Tags:

java

android

I am trying to convert a string to Integer/Float/Double but I got a NumberFormatException.

My String is 37,78584, Now I am converting this to any of them I got NumberFormatException.

How can I convert this string to any of them.

Please help me to get out of this problem.

like image 798
Pari Avatar asked Dec 07 '22 12:12

Pari


2 Answers

You have to use the appropriate locale for the number like

String s = "37,78584";
Number number = NumberFormat.getNumberInstance(Locale.FRENCH).parse(s);
double d= number.doubleValue();
System.out.println(d);

prints

37.78584
like image 52
Peter Lawrey Avatar answered Dec 25 '22 15:12

Peter Lawrey


Replace , by "" blank in string and then convert your numbers

String str = "37,78584";
str = str.replaceAll("\\,","");
like image 23
Nandkumar Tekale Avatar answered Dec 25 '22 14:12

Nandkumar Tekale