user2687553
user2687553

Reputation:

Error convert string "10 000.00" to double in Java

I try to convert this string "10 000.00" to double with method Double.valueOf and I've a input format error:

java.lang.NumberFormatException: For input string: "10 000.00"

How to convert it to double?

Upvotes: 3

Views: 1799

Answers (5)

Tarsem Singh
Tarsem Singh

Reputation: 14199

java.lang.NumberFormatException For input string: "10 000.00"

you have space in your input String "10 000.00"

Use following code to remove the Space from your String

Double.valueOf("10 000.00".replaceAll("[^0-9\\.]", "")) // also Works with Special space like \u00A0

Upvotes: 3

Tuna
Tuna

Reputation: 3005

I see the special-space in your input in the comments above. I think my code will help resolve :D

String number = input.replaceAll("[^0-9\\.]", "");
System.out.println(Double.valueOf(number));

Upvotes: 3

Muhammad Ali Hassan
Muhammad Ali Hassan

Reputation: 192

Double.parse(String); // 

It is static method of Double class will take a string and return a double value. Hope it help

Upvotes: 0

Arnaud Denoyelle
Arnaud Denoyelle

Reputation: 31245

You can use:

Double.valueOf(input.replaceAll("[ ]",""));

in order to remove spaces before trying to convert.

Example :

public static void main(String[] args) {
  System.out.print(Double.valueOf("10 000.00".replaceAll("[ ]","")));
}

Output :

10000.0

Upvotes: 6

Maxim Shoustin
Maxim Shoustin

Reputation: 77910

Sure, you have space between 10 and 000. Remove it before conversion.

Try to add replaceAll:

Double.valueOf(str.replaceAll("[ ]+",""));

Upvotes: 1

Related Questions