• Post Reply Bookmark Topic Watch Topic
  • New Topic

Formatting numbers in a String  RSS feed

 
Murad Iqbal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • 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.
 
Lars Vegas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!