site
stats
By Anish Pillai Anish Pillai Posted under qtp and vbscript tutorials

VBScript Arithmetic functions that can be used in QTP

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

In the previous article, we saw the common string manipulation functions that can be used in QTP. In this article, we will cover the arithmetic or math functions that can be used in QTP. Before we begin with this article, please note that you would not be using arithmetic functions as often as you use the string manipulation functions. In fact, you would notice that there are many math functions that you would not be using at all in any real life projects. This is because of the following two reasons –

  • 1) In the actual projects that you work on, there any not many arithmetic operations that need to be performed as compared to string manipulation functions.
  • 2) Even if there is any need to perform some arithmetic operations, most of them would be taken care of by arithmetic operators in QTP. So there is very little scope for the math function to be of any use.



Let us now begin with the arithmetic functions that are available for your use in QTP.

VBScript Math functions that can be used in QTP

Int function and Fix function

Purpose: You can use both these functions (Int and Fix) to retrieve the integer portion of a number. For example, if you pass a number, say 99.99 to any of these functions, the function will return to you the integer part of the number, which is 99 in this case.

Note:
1) Both these functions don’t round off the number, they just get you the integer part. So 49.99 would return 49 and not 50 when you use Int and Fix functions.

2) The value passed should be a valid numeric expression. If the number passed is null, the value returned is also null.

3) Even if you pass the number in double quotes, the functions will return the integer value. For example, if you pass “23.33” to these functions, the return value will be 23. But if you pass the value “one”, then this will result in an error.

4) The difference between Fix and Int can be seen when you pass a negative number to these functions. If the number is negative, Int returns the first negative integer less than or equal to number, whereas Fix returns the first negative integer greater than or equal to number. For example, if you pass number -10.5 to Int, the value returned will be -11. But if this same number (-10.5) is passed to function Fix, the value returned will be -10.

Syntax: Int(number) and Fix(number)

Example:

'===== Example of Int Function in QTP =====

msgbox Int(99.99) ' msgbox returns value 99
msgbox Fix(99.99) ' msgbox returns value 99

msgbox Int("99.11") ' msgbox returns value 99
msgbox Fix("99.11") ' msgbox returns value 99

Dim iVar
msgbox Int(iVar) ' msgbox returns value 0
msgbox Fix(iVar) ' msgbox returns value 0

msgbox Int(-99.99) ' msgbox returns value -100
msgbox Fix(-99.99) ' msgbox returns value -99


Abs Function

Purpose: Abs function can be used in QTP to return the absolute value of a number. Now, absolute value of any number is its unsigned magnitude. For example, the absolute value of 99.99 is 99.99. And the absolute value of -99.99 is also 99.99 (i.e its unsigned magnitude)

Syntax: Abs(number)

Example:

'===== Example of Abs Function in QTP =====

msgbox Abs(99.99) ' msgbox returns value 99.99
msgbox Abs(-99.99) ' msgbox returns value 99.99

Dim iVar
msgbox Abs(iVar) ' msgbox returns value 0

From this example you can see that you would not be using this function much. Let us suppose you come across a situation where you need to find the positive value of any given negative number. As we had mentioned in the beginning, many of the arithmetic operations can be done using the math operators. In this case also, if you multiply the number with -1, you would get the desired result.

Round Function

Purpose: You can use the Round function to retrieve a number rounded to a specified number of decimal places. For example, if you want the number 1.2468 rounded off to two decimal places, the Round function will return the value 1.25.

Syntax: Round(Number, DecimalPlaces[optional])
If you don’t provide any value for DecimalPlaces, the number will be rounded off to the nearest integer.

Example:

'===== Example of Round Function in QTP =====

msgbox Round(1.2468, 2) ' msgbox returns value 1.25
msgbox Round(1.2468) ' msgbox returns value 1


Rnd Function

Purpose: Rnd function is one of the most common math functions that you will use in QTP. You can use this function to generate a random number in QTP. You can use Rnd function with Randomize statement to achieve the desired results. Check the article – Create random numbers in QTP for more information on how to use Rnd function in real life projects.

Sgn Function

Purpose: Using Sgn function, you can find out the sign of a number. That is, you can use this function to find out if the number is positive or negative. If you pass a number which is greater than 0, the value returned will be 1. If you pass a negative number to this function, the value returned will be -1. And if you pass the number 0, the value returned will also be 0.

Syntax: Sgn(number)

Example:

'===== Example of Round Function in QTP =====

msgbox Sgn(100.12) ' msgbox displays the value 1
msgbox Sgn(-5) ' msgbox displays the value -1
msgbox Sgn(0) ' msgbox displays the value 0

msgbox Sgn("23.00") ' msgbox displays the value 1
msgbox Sgn("-5") ' msgbox displays the value -1
msgbox Sgn("0") ' msgbox displays the value 0

In this case also, the task to find if the number is positive or not can be done using VBScript arithmetic operators. You can pass the number in an If condition to find out if it is positive or negative. Although it involves 5-6 lines of extra code, it isn’t some complex code that justifies the use of Sgn function.

Other less frequently used math functions in VBScript

Exp Function: This function returns e (the base of natural logarithms) raised to a power.

Log Function: You can use the Log function to find the natural logarithm of a number.

Sin Function: Sin function can be used to find out the Sine of an angle.

Cos Function: Cosine function returns the Cosine of an angle.

Tan Function: Tan function returns the tangent of an angle.

Atn Function: Atn function returns the arctangent of a number.


This was all about the VBScript math functions that you can use in QTP. There are not many functions that you would be using from this list. So for your QTP projects, you can use the functions that you remember. For the rest, you can make use of the arithmetic operators which would help you do most of the job. :–)

If you enjoyed this article, you can join our blog to get new articles delivered directly in your inbox.

To check out more tutorials, visit our QTP Tutorials page. You can also check the Archives page to view the list of all our articles.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×