| Author |
Formatting numbers in a String
|
Murad Iqbal
Ranch Hand
Joined: Dec 09, 2003
Posts: 90
|
|
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
Joined: Dec 04, 2001
Posts: 27
|
|
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
Joined: Dec 10, 2001
Posts: 7023
|
|
|
Moving this to the Java in General (beginners) forum...
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
(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 ]
|
 |
 |
|
|
subject: Formatting numbers in a String
|
|
|