jQuery in Action, 2nd edition*
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
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: 10268
    
    8

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: 19680
    
  19

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: 10268
    
    8

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: 38481
    
  23
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: format integer into string with comma