| 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
|
|
|
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...
|
 |
 |
|
|
subject: Preformatted Output
|
|
|