Vbscript Reads - as a Negative Not Dash
Become a Zephyr Scale All-Star, contribute to product development, and get exclusive benefits! Acquire more
This topic contains information about handling numeric data blazon values and provides examples in the following sections:
Basics
The numeric values tin can be of integer and floating-betoken blazon and the TestComplete scripting engine does not distinguish floating-point and integer data types, so the same variable can hold values from both types.
The integer value tin accept nothing, positive and negative numbers within the ±2147483647 range. By and large, the integer number is considered to be in the decimal format, yet octal and hexadecimal representation is also possible.
The octal and hexadecimal numbers can be negative, but cannot be written in the exponential form or cannot have fractional parts.
The integer is treated every bit octal, if information technology is prefixed with "&O" and contains digits from 0 to vii. For example, &O61 is equivalent to decimal 49. The integer is treated as hexadecimal, if it is prefixed with "&H" and contains digits from 0 to 9 or letters from A to F (uppercased or lowercased). The letters from A to F are used to represent numbers from x to 15. For instance, &Hff is equivalent to decimal 255 and &H5EA is equivalent to decimal 1514.
Floating-indicate numbers have a fractional part that can be equally small as ±1.401298^-45. The floating-indicate number can exist inside the ±3.402823^38 range. Mostly, the partial office is separated by the decimal point graphic symbol. For case, 123.456. Another possible notation for the floating point value is scientific or exponential notation. In this annotation the exponent symbol "e" means "10 to the ability of". For case, 37e2 is a scientific note for 3700.
The aqConvert and aqString objects contain several methods that can exist helpful when working with numeric values. The tables below list these methods. The objects are bachelor for all supported scripting languages, and so that you lot can use them to operate with date values regardless of the chosen linguistic communication.
| Method | Description |
|---|---|
| FloatToStr | Converts a floating-bespeak value to a string. |
| Format | Converts a floating-point value to a string using the one of predefined format settings. |
| IntToStr | Converts the given number into a cord. |
| StrToFloat | Converts the specified string to a floating-indicate value. |
| StrToInt | Converts the specified string to an integer value. |
| StrToInt64 | Converts the specified string to a long integer value. |
Furthermore, VBScript has several functions that perform mathematical operations over numbers. The table beneath lists these functions. For a more detailed clarification, see the Math Fuctions commodity in the MSDN library.
| Function | Description |
|---|---|
| Abs(number) | Returns the accented value of a number. |
| Atn(number) | Returns the arctangent of a number. |
| Cos(number) | Returns the cosine of a number. |
| Exp(power) | Returns due east (the base of operations of natural logarithms) raised to the specified power. |
| Fix(number) | Truncates the input value by excluding the fractional office. |
| Int(number) | Returns the integer part of a specified number. If the number is negative information technology is rounded downward. |
| Rnd([number]) | Returns a pseudorandom number between 0 and 1. The parameter value determines how Rnd generates a random number: if information technology is negative, then the same number is generated each fourth dimension; if it is zip, so the recently generated number is returned; if information technology is non specified or is positive, so a new random number is generated. |
| Round(number[, numdecimalplaces]) | Returns a number rounded to a specified number of decimal places. If second parameter is omitted rounds off to integer. |
| Sgn(number) | Returns an integer indicating the sign of a number. |
| Sin(number) | Returns the sine of a number. |
| Sqr(number) | Returns the square root of a number. |
| Tan(number) | Returns the tangent of a number. |
Language-specific operations
VBScript besides has additional arithmetic operators, they are listed in the table:
| Exponentiation (^) | Raises a number to the power of an exponent. |
| Integer division (\) | Calculates the integer event of division for the numbers which are non evenly divisible. |
| Modulo (Mod) | Calculates the balance of the division and is simply concerned with the resulting residuum after division is performed on two operands. If the operands are floating bespeak numbers, so they are rounded to an integer. |
| Sign negation (-) | Requires a single operand. Returns the value of the opposite operand sign. |
This sample code illustrates their employ:
Rounding off
VBScript has several functions that accept a floating bespeak value and return an integer value. These routines are: Fix, Int and Round.
The Fix routine truncates the specified value. The returned value is equal or smaller than the positive input value, and equal or greater than the negative input value. That is, for 1.5 the routine will return ane, and for -one.5 it volition return -1.
The Int routine is similar to Ready, but for negative input values information technology returns the integer value that is smaller than the input value, or in other words it rounds down the input value. For one.v the routine will render i, and for -1.v it will render -two.
The Round routine implements the most common method of rounding, that is too known as symmetric arithmetic rounding. It returns the nearest integer that corresponds to the given floating-point value. If the fractional part of an input value is equal to or greater than 0.5, so the resulting integer is greater than the input value, otherwise - the result is less than the input value. This dominion applies to positive numbers, the same dominion as well applies for negative numbers, but with i difference - the accented values are used instead of bodily input values. That is, for 0.iv the routine will return 0, for 0.five it will return i, and for -0.five it volition return -ane.
Furthermore, you lot tin can use this function to round floating-bespeak values to a specified precision. With the 2nd parameter you can set how many places to the right of the decimal separator are included in the rounding. For case, the Round (3.6297, 2) will result in 3.63.
Here is a sample code that demonstrates how to apply those functions:
Converting to strings
One of the virtually frequent actions over numbers is converting them to strings. This could exist required to post a numerical value to the TestComplete log, output the test event, write data to a text file and in many other situations. For this purpose, aqConvert has 2 methods: IntToStr and FloatToStr. You can also discover the Format method of the aqString object useful.
The IntToStr method accepts an integer value and returns a string belongings its decimal representation. The integer values can be in decimal, octal or hexadecimal form, but the resulting cord is always in the decimal form.
To convert floating-point numbers, utilize the FloatToStr or Format method. The FloatToStr is the simplest: the generated string contains up to 15 digits and the decimal separator is only displayed when required. To specify the format of the resulting string, use the Format method. Information technology provides corking flexibility, since it allows you to set a user-defined formatting string.
The code below illustrates how to employ these methods.
VBScript
Sub NumToStrDemo
intV=17
Log.Message(aqConvert.IntToStr(intV)) 'Posts 17
intV=&Hff
Log.Message(aqConvert.IntToStr(intV)) 'Posts 255
intV=&H47C
Log.Bulletin(aqConvert.IntToStr(intV)) 'Posts 1148
intV=&O31
Log.Message(aqConvert.IntToStr(intV)) 'Posts 25
floatpt=-1234.567890
Log.Message(aqConvert.FloatToStr(floatpt)) 'Posts -1234.56789
Log.Message(aqString.Format("%1.4E",floatpt)) 'Posts -1.2346E+003
FinishSub
Getting numerical values from strings
The aqConvert object has two methods that convert a number out of a string. They are StrToInt, StrToInt64 and StrToFloat.
The StrToInt and StrToInt64 methods accept a string holding a decimal representation of a number and return an integer. The input string can only contain digits and the + or - sign. All other symbols are not immune. If the input string does not hold a valid integer an exception occurs.
To become a floating-signal number from a string utilize the StrToFloat method - it accepts a string that consists of digits, decimal separator, "+" or "-" symbols and mantissa ("e" or "East" character followed by a positive or negative integer) and returns the floating-point number. If the input string does not hold a floating-point number an exception occurs.
Hither is a sample that shows how to use these methods:
VBScript
Sub StrToNumDemo
intV=aqConvert.StrToInt("-1024")
Log.Bulletin(intV) 'Posts -1024
floatpt=aqConvert.StrToFloat("-1234.56789e2")
Log.Message(aqConvert.FloatToStr(floatpt)) 'Posts -123456.789
EndSub
However, sometimes, the functionality of these methods is insufficient, since they take some drawbacks when working with capricious strings. StrToInt, StrToInt64 and StrToFloat methods cannot recognize strings containing characters other than those mentioned higher up. If these methods cannot recognize the string, they enhance an exception.
A versatile routine that would extract numbers from any textual cord and recognize both integer and floating point values can be implemented with the help of regular expressions. The following regular expression pattern would friction match positive or negative integer numbers, also every bit floating-point numbers both in general and scientific notations: [-+]?\d*\.?\d+([eE][-+]?\d+)?.
Below is a sample for a routine that verifies whether a string contains a number. Information technology uses the regular expression to cheque the input string and returns True if the input cord holds an integer or floating bespeak number.
VBScript
Part ContainsNumber(Str)
Dim re
Set re = New RegExp
re.Blueprint = "[-+]?\d*\.?\d+([eE][-+]?\d+)?"'Specify the regular expression
re.IgnoreCase = True' Set case insensitivity.
re.Global = True' Ready global applicability.
ContainsNumber=re.Examination(Str)'Return the verification result
FinishFunction
The aforementioned regular expression can exist used to extract a number from a string. Since the input string can comprise more than ane number matching the regular expression, but the first occurrence would exist returned by the routine. If the string does non hold any number, it is convenient to set the default value that would exist returned in this instance.
VBScript
Function ExtractNumber(Str, DefaultValue)
Dim Matches, re
Set up re = New RegExp
re.Blueprint = "[-+]?\d*\.?\d+([eE][-+]?\d+)?" 'Specify the regular expression
re.IgnoreCase = True' Prepare instance insensitivity.
re.Global = True' Set up global applicability.
Ready Matches = re.Execute(Str) 'Search for occurrences
'If no numbers were institute so return default value
IfNot re.Test(Str) Then
ExtractNumber=DefaultValue
'Else, catechumen a string with kickoff occurrence into a number
Else ExtractNumber=aqConvert.StrToFloat(Matches(0).Value)
CeaseIf
TerminateFunction
Hither is an example of how to use these 2 routines:
VBScript
Sub NumberFromStr
aStr="A value is : -1.234e2"
Log.Bulletin(ContainsNumber(aStr))'Posts True
Log.Message(ExtractNumber(aStr,-50)) 'Posts -123.4
EndSub
Run across Too
Working With Numeric Values
aqConvert Object
Source: https://support.smartbear.com/testcomplete/docs/scripting/working-with/numbers/vbscript.html
0 Response to "Vbscript Reads - as a Negative Not Dash"
Enviar um comentário