aspose 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 Java 8 in Action this week in the Java 8 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: 9995
    
    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: 19556
    
  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: 9995
    
    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: 36590
    
  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
question about Integer constructor
Converting a string to an int
Why are all primitive wrappers immutable? <eom>
Class Cast Exception while reading the int value from a session object
EJB question ... i dont agree with the answer !