• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

formatting negative integers with parentheses

 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic