This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Formatting numbers in a String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Formatting numbers in a String" Watch "Formatting numbers in a String" New topic
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Formatting numbers in a String
 
Similar Threads
inputText customized behaviour (newbie)
printout "�" turns to u`
number formatting????
f:convertNumber not working in dataTable context
How to pretty print ResultSet in a text table