Answered

Round off a number

I have a scenario where @Num_calculated = 1.306 and @Num_actual = 1.31, and I have to do a Compare between these two values but it's failing. Is there any way to round off @Num_calculated to 2 decimal digits in Subject7 ?

0
14 comments

Official comment

Avatar
Payam Fard

Hi Raunak, 

Please try the following:

Math.round(1.306 * 100) / 100

Here is a sample test case to show that both numbers are rounded to 2 decimal (i.e. value of 1.31) and pass the equality assertion just fine:

SET VARIABLE Num_calculated VALUE EXPRESSION "Math.round(1.306 * 100) / 100" HALT ON ERROR
SET VARIABLE Num_actual VALUE EXPRESSION "Math.round(1.31 * 100) /100" HALT ON ERROR
COMPARE @Num_calculated EQUAL @Num_calculated AS TEXT HALT ON ERROR

Please let us know if this solves your issue.

Comment actions Permalink
Avatar
Joseph Hamdan

Hi Raunak,

Could you please provide a screenshot of the command options for Compare that you used and provide a screenshot of the failure?

Regards,

Subject7 Team

0
Comment actions Permalink
Avatar
Raunak Nandy (US - ADVS)

This is the error message:

 

And this is the command options:

0
Comment actions Permalink
Avatar
Raunak Nandy (US - ADVS)

In Set_Var command, for the source type as EXPRESSION, can we use variables using @ to refer to them ? for example: in 'Math.round(1.306 * 100) / 100', can we use @Num in place of 1.306 like 'Math.round(@Num* 100) / 100' ?

0
Comment actions Permalink
Avatar
Joseph Hamdan

Hi Raunak,

Yes, of course you can use variables in Set_Var with expression.

Regards,

Subject7 Team

0
Comment actions Permalink
Avatar
Raunak Nandy (US - ADVS)

I have been trying to use variables in set_var expression type but it throws an error as follows:

0
Comment actions Permalink
Avatar
Joseph Hamdan

Hi Raunak,

Can you run the same steps in a new test case, like below. I just ran it and it passed.

Regards,

Subject7 Team

0
Comment actions Permalink
Avatar
Raunak Nandy (US - ADVS)

I did, but same result:

0
Comment actions Permalink
Avatar
Joseph Hamdan

Hi Raunak,

Could you please restart local player and try again?

If the same issue occurs, please perform Force Update and check again.

Subject7 Team

0
Comment actions Permalink
Avatar
Payam Fard

Raunak, what version of Java are you using?

0
Comment actions Permalink
Avatar
Raunak Nandy (US - ADVS)

I am using OpenJDK version 15.0.1

0
Comment actions Permalink
Avatar
Payam Fard

Please use Java 8 or 11 instead. This issue has to do with Java 15. Please update this thread once you downgrade your Java version.

0
Comment actions Permalink
Avatar
Raunak Nandy (US - ADVS)

I downgraded to Java 11.0.2 and now it's working fine, thanks for your support !

0
Comment actions Permalink
Avatar
Joseph Hamdan

Hi Raunak,

You are most welcome, we're glad it's working.

Regards,

Subject7 Team

0
Comment actions Permalink

Please sign in to leave a comment.