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?

2
  • possible duplicate of Parsing a currency String in java Commented Sep 25, 2015 at 1:33
  • You really should spend a few minutes googling your own question before posting. Commented Sep 25, 2015 at 1:33

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

You can do the following:

Double.parseDouble(string.replaceAll("[$]", "")); 

Comments

0

Hope this would help,

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.