aspose file tools*
The moose likes Beginning Java and the fly likes setMinimumIntegerDigits problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "setMinimumIntegerDigits problem" Watch "setMinimumIntegerDigits problem" New topic
Author

setMinimumIntegerDigits problem

kunagu varun
Ranch Hand

Joined: Apr 13, 2010
Posts: 38
Hello ranchers
I am currently facing a problem in displaying integers in my desired format. So please help me.

what is actually need is i want my program to print integers with zeroes padded in front of a number if it contains less than 4 digits.
i.e if num=123 then the value must be changed to 0123.

So i have used NumberFormat class and methods to achieve this and especially setMinimumIntegerDigits(4);
the following is my code.


when i run this i got the output as 123 and what i need is to automatically changed to 0123
please help me in solving out this issue guys..

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Check out DecimalFormat in combination with #.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You're just declaring an int, and then printing it. You never called nf.format(no).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
kunagu varun
Ranch Hand

Joined: Apr 13, 2010
Posts: 38
Thanks a lot for your replies frineds
As you said i have modified the System.out.print as follows


When i run this i get output as 0,123. I dont want that comma to appear. What should i have to do???
kunagu varun
Ranch Hand

Joined: Apr 13, 2010
Posts: 38
Ok guys i have solved the remaining comma part by myself.
I have found out the following thing from java documentation.


Now the output appears as i desired.

Thank you very much for helping me..
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I like to use String.format because of its compactness (and I'm an old C programmer)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

There are different ways to get there, but as Rob suggested above, DecimalFormat is probably the cleanest choice.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

It would be before java.util.Formatter and its friends String.format and PrintStream.printf were introduced. Now they are in a lot of cases interchangeable. I like Tom's solution as well.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setMinimumIntegerDigits problem