You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Janos Mucsi <mu...@yahoo.com> on 2005/04/20 06:00:36 UTC

fixed length fields

Hi
I need to have fixed length fields. Is there a way in
Velocity to format the output of a variable so it is
padded with whitespaces (in the output) to a specific
length.
Thanks.
Janos

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: fixed length fields

Posted by jian chen <ch...@gmail.com>.
Hi, 

Just a thought. Could pass in the java.util.Formatter class to the
Velocity context and use it for formatting?

Cheers,

Jian 

On 4/19/05, Janos Mucsi <mu...@yahoo.com> wrote:
> Hi
> I need to have fixed length fields. Is there a way in
> Velocity to format the output of a variable so it is
> padded with whitespaces (in the output) to a specific
> length.
> Thanks.
> Janos
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: fixed length fields

Posted by Christoph Reck <ap...@recks.org>.
Hi,

I used a macro for zero-padding, which can easily be modified to space-padding:
#macro( fourdigits $number )#*
   *##* #local( $id $len ) *##*        -- optional directive, avoids polluting context
   *##set( $id = "$number" )#*         -- access the macro paramter, ensure its a String
   *##set( $id = "0000$id.trim()" )#*  -- prepend the padding, clipping whitespace artefacts
   *##set( $len = $id.length() - 4 )#* -- compute the offset for the rest length
   *#$id.substring($len)#*             -- emit the resulting padded string
   *##* #end *###                      -- close the optional #local directive
#macro

See original thread in:
   http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg14419.html

Also see other useful macros in the wiki:
   http://wiki.apache.org/jakarta-velocity/VelocityGenericMacros

But beware of the velocity whitepace handling...
To avoid extra whitepaces either place everything on the left
margin (no indentation) or do asin above macro example with
block comments and eol-comments.

Cheers,
:) Christoph Reck


Janos Mucsi wrote:
> Hi
> I need to have fixed length fields. Is there a way in
> Velocity to format the output of a variable so it is
> padded with whitespaces (in the output) to a specific
> length.
> Thanks.
> Janos
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org