• Post Reply Bookmark Topic Watch Topic
  • New Topic

formatting negative integers with parentheses  RSS feed

 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to format negative and positive integers like 23974 and -98732 in this way:

23,974
(98,732)

The positive ones aren't that hard:


For the negative ones, this seems to work for me:



However, the DecimalFormat doc advises not to call the DecimalFormat constructors directly. Is there another/better way to do this?

Also, it looks like I can't do the parentheses for negatives on percents, because I only know how to call this:



And NumberFormat doesn't have those methods I used above...
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37489
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen,

However, the DecimalFormat doc advises not to call the DecimalFormat constructors directly. Is there another/better way to do this?

For decimal format, you could use "postivePattern ; negativePattern", right?

I'm not sure how to handle it for the percentages without custom logic. There must be a way though.
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Y'know, I saw that, but I wasn't sure what it was talking about. I think I was a bit too tired, because I just played some basketball and looked at that again, and I see how to use that now!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!