File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 9990
    
    7

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: 19543
    
  16

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: 9990
    
    7

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: 36482
    
  16
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
 
Similar Threads
EJB question ... i dont agree with the answer !
question about Integer constructor
Converting a string to an int
Class Cast Exception while reading the int value from a session object
Why are all primitive wrappers immutable? <eom>