• Post Reply Bookmark Topic Watch Topic
  • New Topic

DecimalFormat  RSS feed

 
nikki mateti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a question. I need to format strings which are numbers into strings with 2 decimal places. But if the number has no decimal, then only the integer part should be displaced.

For eg
75 -> 75
75.5 -> 75.50
75.75 -> 75.75


I tried DecimalFormat twoDigits = new DecimalFormat("0.00");
but it displays
75 as 75.00

Do let me know how I can do it.
Thanks,
nikki
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After you've formatted the number, use String methods to check whether it ends with ".00" and remove the last 3 chars if it does.
 
nikki mateti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to do it without parsing using NumberFormat or DecimalFormat classes?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to what Mark said, you could create your own class like MyDecimalFormat which is an extension of DecimalFormat and adds the capability of removing the 0s.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!