• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Formatting numbers in a String

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,


I need to format a number into a String. The pattern should be of 4 digit. How can I achieve that? For example if value of an int is 1, I should get 0001 after formatting and if there is 123, I should get 0123.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this out:

http://www.particle.kth.se/~lindsey/JavaCourse/Book/Code/Tech/Formats/CJ/com/horstmann/format/Format.java

Just change the printf-Methods, that they return the wanted value.
With this changed class you can write something like:

Format.printf( "%4d", YourLongValue );

Hope this helps
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Java in General (beginners) forum...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(This should probably be in our FAQ.)

Take a look at the java.text.DecimalFormat.

Were you able to figure it out? If you'd like another nudge in the right direction, just ask. Do feel free to post any relevant code you're working on.
[ September 02, 2004: Message edited by: Dirk Schreckmann ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic