Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Right Justify---zero fill  RSS feed

 
Mike Kay
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the user filling in a HTML text box in my JSP a serial number string like 1234.
The length of the field is zero filled in my database so I need to put a 0 in front like 01234. Is this possible?

I am building a SQL statement with the user selects like this:
sysserno = request.getParameter("sysserno");
sqlValue=sysserno;
sqlName="SYSNO";
sqlCondition=sysserCondition;
buildSQL();


what can I do to sysserno to make sure it's 5 characters zero filled?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the DecimalFormat class.
 
Mike Kay
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnx
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that a String (char of some kind) in the database? For a quick zero pad to a fixed length - say five as you showed above - you can do:

String padded = ( "00000" + input ).substring( input.length() );

If you do this a lot you might make a utility method that takes a length and a pad character. This signature come from the REXX language; right is short for right justify. In REXX right() left() and center() also truncate inputs that are too long so you can make neat columnar output.

right( input, length, pad )
right( "123", 5, "0" ) -> "00123"
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!