Thanks:  0
Likes:  0
Dislikes:  0

# Thread: arithmetic on captured fields

1. ## arithmetic on captured fields

I am attempting to do simple arithmetic on captured values from the screen. These are stored in Variables. However Winrunner recognises these are string instead of integers producing odd results. Any ideas how strings can be converted to integers?

2. ## Re: arithmetic on captured fields

If the variable contains a valid number, WR will treat it as such. There is no need to perform conversion.

Perhaps if you give us some more information on your "odd results" we can give more specific suggestions.

3. ## Re: arithmetic on captured fields

WR freely switches variable context from numeric to string. However, if there are any extra characters captured into your variable (including commas) then it might do unexpected things when you try to treat it as a number. (Basically, WR will read the string from left to right until it gets "confused", then use whatever it read to that point as the numeric value. Therefore "123", "123test" and "123,456.789" would all be "equal" when processed in a numeric context.) Therefore, if you read numeric text from a screen and then want to process it arithmetically, you have to make sure you have stripped out any "confusing" characters.

Also be aware that the floating-point arithmetic used by WR can sometimes give low-order rounding errors that can be confusing. The simplest solution for that is to round any results to the number of significant digits with which you are concerned, for instance...

total = sprintf("%.2f", 123.45 + 234.56);

...to make sure you only get 2 decimal places in "total".

4. ## Re: arithmetic on captured fields

I have had some problems in the past with calculating too.
Functions (not standard, but self created) that were most helpfull for solving these problems are

is_numeric(in strString); # determining if it is a number

is_integer(in strString); # determining if it is an integer

replace(in strText, in strToReplace, in strReplaceBy) # for replacing . by , or , by . (depending on how values are setup 1,000,000.5 for 1 million and a half or 1.000.000,5 or 1000000,5)

round(in nNumber,in nDecimals); # rounding...

of course, when capturing a value out of a field, what you can do is multiply it by 1. If this is the same as the original (unmultiplied) string, you can bet it is a number.

e.g.
strText="14.0" # captured from field
if (strText==1*strText)
{
# should be a number
}

I noticed some parculiar things in the past, but if you take all conversion into your own hands (functions) you'll solve the problem.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.