File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setMinimumIntegerDigits problem

 
kunagu varun
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out DecimalFormat in combination with #.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're just declaring an int, and then printing it. You never called nf.format(no).
 
kunagu varun
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to use String.format because of its compactness (and I'm an old C programmer)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are different ways to get there, but as Rob suggested above, DecimalFormat is probably the cleanest choice.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic