T3RON
T3RON

Reputation: 65

How to convert a string with multiple points to double in android

I have a string "3,350,800" with multiple points I want to convert to double but have error multiple points

String number = "3,350,800"
number = number.replace(",", ".");
double value = Double.parseDouble(number);

Error : java.lang.NumberFormatException: multiple points

Upvotes: 3

Views: 6740

Answers (4)

Ram
Ram

Reputation: 115

Mix of other answers, no reason to change the , for . and then fetch the German local.

String number = "3,350,800";
NumberFormat format = NumberFormat.getInstance();
double value = format.parse(number).doubleValue();
System.out.println(value);

Output:

3350800.0

Upvotes: 2

user2862544
user2862544

Reputation: 425

you need to use something like this :

String number = "3,350,800";
number = number.replaceAll(",", "");
double value = Double.parseDouble(number);
System.out.println(value);

Upvotes: 1

Mureinik
Mureinik

Reputation: 311073

The . character is used as a decimal point in English, and you cannot have more than one of those in a number.

It seems like you're using it as a thousands separator though. This is legal in several locales - you just need to use one that allows it, e.g.:

String number = "3.350.800";
NumberFormat format = NumberFormat.getInstance(Locale.GERMAN);
double value = format.parse(number).doubleValue();

Upvotes: 12

Shai
Shai

Reputation: 111

What number are you trying to get?
3.350.800 is what you're trying to parse as a double,
but that's obviously not a number, since there are "multiple points".

If you just wanna get 3,350,800 as your number, simply change this line -
number = number.replace(",", ".");

to this -
number = number.replace(",", "");

Upvotes: -1

Related Questions