cancel
Showing results for
Did you mean:

## Code Expression

SOLVED
Frequent Contributor

## Code Expression

Hi all

I've used the below code expression in my test:

"aqConvert.StrToInt(ProjectSuite.Variables.XX) * aqConvert.StrToInt(ProjectSuite.Variables.YY)"

The above code is suppose to multiply the values in 'variable.xx' & 'variable.yy' however I am unable to get the expected value which equals actual value. Screenshot examples below:

Variable XX & YY ValuesTest Run Result

In the above scenario, when you calculate 3.257*354.66, the expected value must be 1155.13 however I am not sure why test complete is calculating it to 1065. Any help on this will be much appreciated.

Thanks

GD

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

## Re: Code Expression

Hi,

> now I am stuck with the rounding issue.

Well, I still assume that you are dealing with money (i.e. currency data type)...

There is no direct support for currency in DelphiScript. Below are quick ideas that came first to my head:

-- Use already mentioned Round() function that seems to implement bank rounding rules and use it to create your own code to handle currency. As Round() rounds to integers, you will need to shift calculation base to the right as required, do calculations and than return back. E.g.:

for the initial 3.257 * 354.66 = 1155.13 you need something like this (100 multiplier/divider is used because we are rounding to two digits after comma):

(3.257 * 354.66) * 100 = 115512.762 ==> Round(115512.762) = 115513 ==> 115513 / 100 = 1155.13

-- Ask your developers (or do it yourself) to create a DLL that conforms to TestComplete's requirements (see help for the DLL object) and implements currency calculations. Then call functions provided by this DLL;

-- If .Net is installed on the computers where TestComplete tests are executed, then investigate if .Net provides currency calculation routines that can be called from TestComplete via the dotNET object and use them (https://stackoverflow.com/questions/2215601/money-data-type-for-net ?);

-- Create a Script Extension in TestComplete using some already existing VBScript or JScript code/library and use this Script Extension from your code. (https://frontstuff.io/how-to-handle-monetary-values-in-javascript, http://openexchangerates.github.io/accounting.js/)

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
8 REPLIES 8
Community Hero

## Re: Code Expression

This happens because you are using INT as a type. Actually your code doing now: 3 * 355 = 1065

Try aqConvert.StrToFloat() method

Community Hero

## Re: Code Expression

Hi,

You are converting values to integers before multiplication. So the actual expression becomes:

3 * 355 which equals to 1065.

Note, that you are working with floating values. The exact result of multiplication done manually on paper is:

3.257 * 354.66 = 1155.12762

and you need to consider correct rounding in your verification code if you need to get rounded value of 1155.13

I am guessing that you are working with the values of money type. I don't know what scripting language your test project is based on, but neither TestComplete itself, nor VBScript explicitly supports money calculations and you need to search for some sample code that implements correct roundings.

I am not sure about JScript/JavaScript/Python - it is possible that they have internal support for money data type.

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Frequent Contributor

## Re: Code Expression

Hi

The test project is based on Delphi and you were right, now I am stuck with the rounding issue.

I am not sure what verification code will help me with rounding the expected value to "1259.13". Any suggestions ?

Thanks

Community Hero

## Re: Code Expression

Try Round() function >>

UPD. ups, Round returns an integer value... Try native Delphi function RoundTo() or SimpleRoundTo().

Frequent Contributor

## Re: Code Expression

@baxatob wrote:

Try Round() function >>

UPD. ups, Round returns an integer value... Try native Delphi function RoundTo() or SimpleRoundTo().

I am using a code expression here and unfortunately it does not give me option to try native delphi functions

Community Hero

## Re: Code Expression

Try to round required values before they were assigned to ProjectSuite variables.
Community Hero

## Re: Code Expression

Hi,

> now I am stuck with the rounding issue.

Well, I still assume that you are dealing with money (i.e. currency data type)...

There is no direct support for currency in DelphiScript. Below are quick ideas that came first to my head:

-- Use already mentioned Round() function that seems to implement bank rounding rules and use it to create your own code to handle currency. As Round() rounds to integers, you will need to shift calculation base to the right as required, do calculations and than return back. E.g.:

for the initial 3.257 * 354.66 = 1155.13 you need something like this (100 multiplier/divider is used because we are rounding to two digits after comma):

(3.257 * 354.66) * 100 = 115512.762 ==> Round(115512.762) = 115513 ==> 115513 / 100 = 1155.13

-- Ask your developers (or do it yourself) to create a DLL that conforms to TestComplete's requirements (see help for the DLL object) and implements currency calculations. Then call functions provided by this DLL;

-- If .Net is installed on the computers where TestComplete tests are executed, then investigate if .Net provides currency calculation routines that can be called from TestComplete via the dotNET object and use them (https://stackoverflow.com/questions/2215601/money-data-type-for-net ?);

-- Create a Script Extension in TestComplete using some already existing VBScript or JScript code/library and use this Script Extension from your code. (https://frontstuff.io/how-to-handle-monetary-values-in-javascript, http://openexchangerates.github.io/accounting.js/)

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Frequent Contributor

## Re: Code Expression

@AlexKaras wrote:

Hi,

> now I am stuck with the rounding issue.

Well, I still assume that you are dealing with money (i.e. currency data type)...

There is no direct support for currency in DelphiScript. Below are quick ideas that came first to my head:

-- Use already mentioned Round() function that seems to implement bank rounding rules and use it to create your own code to handle currency. As Round() rounds to integers, you will need to shift calculation base to the right as required, do calculations and than return back. E.g.:

for the initial 3.257 * 354.66 = 1155.13 you need something like this (100 multiplier/divider is used because we are rounding to two digits after comma):

(3.257 * 354.66) * 100 = 115512.762 ==> Round(115512.762) = 115513 ==> 115513 / 100 = 1155.13

-- Ask your developers (or do it yourself) to create a DLL that conforms to TestComplete's requirements (see help for the DLL object) and implements currency calculations. Then call functions provided by this DLL;

-- If .Net is installed on the computers where TestComplete tests are executed, then investigate if .Net provides currency calculation routines that can be called from TestComplete via the dotNET object and use them (https://stackoverflow.com/questions/2215601/money-data-type-for-net ?);

-- Create a Script Extension in TestComplete using some already existing VBScript or JScript code/library and use this Script Extension from your code. (https://frontstuff.io/how-to-handle-monetary-values-in-javascript, http://openexchangerates.github.io/accounting.js/)

It worked !!

It seems that mathematical calculation did the trick. I used the below code expression:

Round(aqConvert.StrToFloat(ProjectSuite.Variables.Price) * aqConvert.StrToFloat(ProjectSuite.Variables.Units)*100)/100

Thanks for your help in sorting this out.

New Here?
Watch the new Interview
Top Kudoed Authors
Latest Topics
Top Kudoed Posts