• Post Reply Bookmark Topic Watch Topic
  • New Topic

Delete a char in a String  RSS feed

 
Greenhorn
Posts: 24
Chrome Google App Engine Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a String that always have the symbol €.
But I need to do calculations and first I need to change it to double.
How can I delete that symbol of the String?
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?
Look at the methods of String class here.
At least one of them can do what you want.

Another approach is to use NumberFormat class.
 
Mikasa Jaeger
Greenhorn
Posts: 24
Chrome Google App Engine Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying this:

String aux = m_oTicket.printTotalBouquet();
aux = aux.replace("€", "");
aux = aux.replace(" ", "");
parcial_bouquet = Double.parseDouble(aux);

But it gives me an Exception:

java.lang.NumberFormatException: For input string: "0,00"
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are passing a String "0,00" with a comma to the parseDouble method.
Apparently your default Locale expects something in format "0.00" with a dot.

You have two options.
1. replace a comma with a dot - ugly
2. use NumberFormat - nice

An advice not related to the problem:
use aux = aux.trim() instead of aux = aux.replace(" ", "");
It looks nicer ;)

Also, are sure you want to use double for calculating currency? Have you considered using BigDecimal?
 
Mikasa Jaeger
Greenhorn
Posts: 24
Chrome Google App Engine Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using aux.replace because the format that I get the String is: #,## €
The whitespace between the number and € isn't get deleted by trim, or I'm wrong?


And how do you recommend me to use NumberFormat to change that ',' into a '.'?
I was using aux.replace and you said it's ugly xD
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not telling you that NumberFormat will change "," to "."
I'm telling you that using NumberFormat will let you parse the String without that change.

Why is 1st option ugly?
You change "," to "." and run your program on JVM where default locale expects ",". And now what?
You'll get NumberFormatException again.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!