aspose file tools
The moose likes Java in General and the fly likes Preformatted Output Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Preformatted Output" Watch "Preformatted Output" New topic
Author

Preformatted Output

Arthur Buliva
Ranch Hand

Joined: Mar 08, 2006
Posts: 101
Suppose you have a method that returns numbers. The numbers have different lengths; that is String.valueOf(number).trim().length() returns different values.

However, I would like to preformat the numbers so that always the length being returned is four. This is what I mean:

Suppose you are returned to the number 123. I need to preformat it to 0123. This retains the value 123 but makes the length to be 4

Suppose this time round its 1. Preformatted should now be 0001. 1024 should remain 1024 as it already is 4 characters long.

How is this achievable?

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Look at java.text.NumberFormat, and its subclass DecimalFormat.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
There's also the String.format() method since Java 1.5


Joanne
Arthur Buliva
Ranch Hand

Joined: Mar 08, 2006
Posts: 101


The hard way...
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Preformatted Output
 
Similar Threads
how to clear the stack
using byte array to copy contents of one file to another
Incorrect value of long calculation?
Phone number formatter tag
Clarification on OOP-4