This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes format integer into string with comma Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "format integer into string with comma" Watch "format integer into string with comma" New topic
Author

format integer into string with comma

mark I thomas
Ranch Hand

Joined: Apr 07, 2008
Posts: 86
How to convert an Integer like one million(1000000) into string like "1,000,000" ? Thanks
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10486
    
    9

Get the string representation by Integer.toString() or String.valueOf(int)
Then use the NumberFormat#parse()

Edit: My bad. Use NumberFormat.getIntegerInstance() .format()


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Maneesh, aren't you getting it the wrong way around? You need to format with a NumberFormat, not parse.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10486
    
    9

Thanks Rob. I had got the wrong end of the stick. I have edited my original post to reflect this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
System.out.println(String.format("%,d", 1000000));

That will give different formats in different countries. Try different Locales; there is an example for France here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: format integer into string with comma