如何使用 Beanshell post 处理器在 Jmeter 中仅打印小数点后 4 位数字?
How can I print only 4 digits after decimal in Jmeter using Beanshell post processor?
我只想将输出四舍五入到小数点后 4 位
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 )
输出
BeanShellTestElement : 44.211143
BeanShellTestElement : 44.211143
预期输出
BeanShellTestElement : 44.2111
BeanShellTestElement : 44.2111
如果您需要字符串表示 - 尝试 DecimalFormat
对于数字表示,请选择 BigDecimal class:
new BigDecimal(a).round(4)
另请注意,starting from JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting 所以考虑迁移。
我只想将输出四舍五入到小数点后 4 位
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 )
输出
BeanShellTestElement : 44.211143
BeanShellTestElement : 44.211143
预期输出
BeanShellTestElement : 44.2111
BeanShellTestElement : 44.2111
如果您需要字符串表示 - 尝试 DecimalFormat
对于数字表示,请选择 BigDecimal class:
new BigDecimal(a).round(4)
另请注意,starting from JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting 所以考虑迁移。