You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jmeter.apache.org by Peter Lambrechtsen <pe...@crypt.co.nz> on 2013/03/28 21:24:10 UTC

Zero Padding in JMeter using Beanshell

I recently had a need to create a zero padded value in JMeter and thought I
would share the Beanshell code I used to get it working.

I wasn't sure why I needed to put the long into a Java Array to make it
work with String.format but this worked for me using a Beanshell Sampler
taking a counter:

String inputcounter = vars.get("InputCounter");
Long[] longarray = new Long[] {Long.valueOf(inputcounter)};
String paddednumber = String.format("%019d", longarray);
vars.put("PaddedCounter",paddednumber);

This will create a padded number up to 19 digits "%019d" change the 19 to
the number of padded digits you need.

This way I could have a JMeter counter with the reference / variable name
"InputCounter" inside a loop, use that value and also create a padded
version of the number as "PaddedCounter".

I am sure there are cleaner ways of doing it but this worked for me :)

Cheers

Peter

Re: Zero Padding in JMeter using Beanshell

Posted by Flavio Cysne <fl...@gmail.com>.
Tested in JMeter 2.9

Use a Counter (under Configuration Elements) and use 0000000000000000000 as
Numer Format. padding as expected.

Hope it helps.

Regards


2013/3/28 Peter Lambrechtsen <pe...@crypt.co.nz>

> I recently had a need to create a zero padded value in JMeter and thought I
> would share the Beanshell code I used to get it working.
>
> I wasn't sure why I needed to put the long into a Java Array to make it
> work with String.format but this worked for me using a Beanshell Sampler
> taking a counter:
>
> String inputcounter = vars.get("InputCounter");
> Long[] longarray = new Long[] {Long.valueOf(inputcounter)};
> String paddednumber = String.format("%019d", longarray);
> vars.put("PaddedCounter",paddednumber);
>
> This will create a padded number up to 19 digits "%019d" change the 19 to
> the number of padded digits you need.
>
> This way I could have a JMeter counter with the reference / variable name
> "InputCounter" inside a loop, use that value and also create a padded
> version of the number as "PaddedCounter".
>
> I am sure there are cleaner ways of doing it but this worked for me :)
>
> Cheers
>
> Peter
>