Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Removing commas from a string to make a double or Double object?  RSS feed

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the best way?

String amt = el.getAttributeValue("amt");
double amtVal = 0.0;

amtVal = Double.parseDouble(amt.trim());


amt.trim() needs to be replaced with something like:

amt.trim().replace("," , ""); //This won't compile...

I have heard something about the DecimalFormat, but I don't know if this applies..
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
replace only works with chars. "," is a String literal.

Try replaceAll.
[ April 20, 2006: Message edited by: Keith Lynn ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to take a look at the NumberFormat and DecimalFormat classes. These will help a lot in solving this particular problem.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!