-2

I have a problem. I have String with the value "$615.00" and I want to convert it to double or int. I have tried the following code but there is an error:

String one = "$615.00";
String two = "$15.00";
double newone = Double.parseDouble( one );
double newtwo = Double.parseDouble( two );

System.out.println(newone-newtwo);

The error is

Exception in thread "main" java.lang.NumberFormatException: For input string: "$615.00"

But I have added the NumberFormatException for the method and still got the error.

deHaar
  • 17,687
  • 10
  • 38
  • 51
Cipri Macavei
  • 17
  • 1
  • 2
  • 1
    Does this answer your question? [Convert String to double in Java](https://stackoverflow.com/questions/5769669/convert-string-to-double-in-java) You are correct... however the $ cannot be used. I recommend you remove the $ before doing the conversion – D. Lawrence Dec 11 '19 at 13:40
  • 1
    Without specifying further, my guess is that `Double#parseDouble(String)` can't handle the `'$'` character. Remove that from the string when you pass it in – Frontear Dec 11 '19 at 13:42
  • What do you mean by *I have added the NumberFormatException*? you don't catch it anywhere. – Guy Dec 11 '19 at 13:42
  • You have them as `String` and you need to remove the currency symbol... Consider using a `BigDecimal`, which takes a `String` in a parameterized constructor and is able to provide a `doubleValue()`. – deHaar Dec 11 '19 at 13:42
  • There are some answers to your question posted here. Let us know, if they helped you – Steyrix Dec 11 '19 at 14:19

3 Answers3

2

As others have said in the comments, NumberFormatException is happening because you are trying to parseDouble without removing the $ from the number.

In this case, you can use substring() to get everything after the first char:

String one = "$615.00";
String two = "$15.00";

double newone = Double.parseDouble( one.substring(1) );
double newtwo = Double.parseDouble( two.substring(1) );

System.out.println(newone-newtwo);

Results in 600.00

João
  • 449
  • 4
  • 18
  • And what if there will be spaces, digits or other symbols (more than one and on another positions)? For me this is very hardcoded solution – Steyrix Dec 11 '19 at 14:06
  • Then you'd need a more complex solution, but for any prefixes or sufixes you need to remove, this works – João Dec 11 '19 at 14:10
  • If the dollar symbol will be a suffix, or there will be 2 options for its position, you will need to change the code and add additional checkings. In other words excess code. This is a major drawback of the solution. You need to write the code in a way, that will provide robust solution with possible changing input data formats – Steyrix Dec 11 '19 at 14:15
  • I agree, your answer with the regular expression is more complete, but I though a simpler solution could help better in this case, since regular expressions are kinda tricky – João Dec 11 '19 at 14:23
  • 1
    Well, assuming the author of question is a beginner, it might be easier and more convenient for him to use your solution. So you do have a point and your answer makes sense :) – Steyrix Dec 11 '19 at 14:25
1

$ is a currency designator. It is not part of a numeric value.

If you have a currency value, you should use a currency format to read it:

NumberFormat format = NumberFormat.getCurrencyInstance();
double newone = format.parse(one).doubleValue();
double newtwo = format.parse(two).doubleValue();

If you are not running on a computer configured for the US, you may need to pass a Locale, to force the currency instance to use US dollars:

NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
double newone = format.parse(one).doubleValue();
double newtwo = format.parse(two).doubleValue();
VGR
  • 40,506
  • 4
  • 48
  • 63
0

Use regular expression to remove symbols like "$" (in other words, all symbols but digits and dot)

String one = "$615.03";
String oneValue = one.replaceAll("[^0-9.]", "");
System.out.println(oneValue); // Output is 615.03, which is correctly parsed by parseDobule()
Steyrix
  • 2,796
  • 1
  • 9
  • 23
  • Using a regular expression is overkill. Regular expressions are heavyweight, and hard to read and debug. They should not be used unless they are truly needed. – VGR Dec 11 '19 at 14:56
  • @VGR I don't think that debugging and reading `if-else` statements is easier than reading the single line with pattern. Also, just a single possibility of input data becoming "615.00$" makes it easier to use regex. I would like to know about a better solution for preprocessing strings, since I got voted down – Steyrix Dec 11 '19 at 15:01