velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christoph Reck <apa...@recks.org>
Subject Re: File format contains the fixed size s# with left padded 0, can we do this in Velocity?
Date Fri, 18 Mar 2005 13:29:55 GMT
You can also use plain VTL:

#set( $id = "0000$number" )
#set( $len = $id.length() - 4 )
$id.substring($len)

To simplify the use, you can create a macro for this:

#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

Use it:
   #fourdigits( 123 )
   #fourdigits( "$myObject.value" )
   #set( $paddedId = "#fourdigits( $number )" )

Disclaimer, this code was stripped out from a tested application,
the snippet itself is untested and provided as a starting point.
If you find this useful, or if you have tested corrections,
please reply to this thread. Thereafter we can take this up into
the WIKI macro page.

Cheers,
:) Christoph

Gagan 007 wrote:
> I have to show some file in this format
> 
> S#  Value
> 
> 0001 abc
> 0002 adc
> 0003 afc
[snip]
> 0017 ab4
> 0018 a3c
> and so on
[snip]
> 
> I have all the values  in the collection and i m iterating through it...
> Plus i need to show the S#... which gets incremented with each value...as shown above...
> 
> The size of the sequence no.. is 4 digits.. if there are not 4 digits then the s# is
left padded with 0
> u can c the example above...
> 
> 
> Is there any feature in Velocity to generate this S# while i m iterating through the
values...
> 
> 
> Its urgent any help would be appreciated..

--
Christoph

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


Mime
View raw message