Arthur Buliva

Ranch Hand

Posts: 101

posted 10 years ago

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.

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.

posted 10 years ago

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

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

Joanne Neal

Rancher

Posts: 3742

16

Don't get me started about those stupid light bulbs. |