Search code examples
jmeterbeanshell

How can I print only 4 digits after decimal in Jmeter using Beanshell post processor?


I just want the output to be rounded off up to 4 decimal places

n1 = 904.56
n2 = 20.46
float a = ${n1} / $(n2};
float b = 0;
log.info(a + "");
b = a;
vars.putObject("b" , a);
log.info( b )

OUTPUT

BeanShellTestElement : 44.211143
BeanShellTestElement : 44.211143

EXPECTED OUTPUT

BeanShellTestElement : 44.2111
BeanShellTestElement : 44.2111

Solution

  • If you need string representation - try DecimalFormat

    enter image description here

    For the numeric representation go for BigDecimal class:

    new BigDecimal(a).round(4)
    

    Also be informed that starting from JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting so consider migrating.