• Post Reply Bookmark Topic Watch Topic
  • New Topic

Preformatted Output  RSS feed

 
Arthur Buliva
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at java.text.NumberFormat, and its subclass DecimalFormat.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also the String.format() method since Java 1.5
 
Arthur Buliva
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The hard way...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!