Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes int formatting??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "int formatting???" Watch "int formatting???" New topic

int formatting???

Kate Zoy
Ranch Hand

Joined: Feb 11, 2004
Posts: 33
i have an int (unknown lenght) that i need to format so there's a comma every 3 digits, like "1,000,203,2203". I know about DecimalFormat df1 = new DecimalFormat(), but I'm not quite sure what to put in the parentheses. If I knew the int was 4 digits, I could do "0,000", but I'm not sure how to go about formatting an unknown number of digits. Let me know!
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
You're on the right track, just use #'s instead of 0's in you DecimalFormatter, like this:
If the number is more than 6 digits, it will automatically stick in another "," at the right places so you don't have to guess at the size.
Try it out and you'll see what I mean.

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
I agree. Here's the link:
subject: int formatting???
It's not a secret anymore!