2

I want to convert a string (for example $4.50) to a double. I understand I can use Double.parseDouble() but I assume this works only if the string does not contain any other chars (such as the dollar sign). The purpose is to compare two Strings (which contain dollar values plus a dollar sign) and determine which one is greater and which one is smaller

How can I convert such a string to a double?

Net Nanny
  • 53
  • 3
  • 10
  • possible duplicate of [Parsing a currency String in java](http://stackoverflow.com/questions/6016501/parsing-a-currency-string-in-java) – JeffC Sep 25 '15 at 01:33
  • You really should spend a few minutes googling your own question before posting. – JeffC Sep 25 '15 at 01:33

4 Answers4

2

Use Double.parseDouble after removing the unwanted characters like currency symbols.

Double.parseDouble(string.replaceAll("[^\\d.]", "")); 

This would removed any character but not of a dot or a digit.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1

Try this Double.parseDouble(yourString.substring(1))

0

You can do the following:

Double.parseDouble(string.replaceAll("[$]", "")); 
karthik manchala
  • 13,492
  • 1
  • 31
  • 55
0

Hope this would help,

Double.parseDouble(string.replaceAll("[^\\d.]", "")); 
Vignesh Shiv
  • 1,129
  • 7
  • 22