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:
[snip]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
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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]