Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int formatting???

 
Kate Zoy
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
Thanks!
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic