Visual Basic 6 Library Functions
Visual Basic 6 Library Functions
These are the Built in Functions provided by Visual Basic. VB offers a rich set of
built-in-functions for manipulating strings, numbers, dates and time. Built in
functions are important and useful as they cut down effort and time, if one has to
write the entire program all over.
This tutorial is going to cover some of the important and commonly used numeric
functions available in the Visual Basic Library.
Numeric Functions
VB supports many mathematical or numeric functions that can make calculations
very simple, as you just have to feed in the variables , and get the output after the
function processes it.
1. Val Function
This function is used as a converter function. It converts the numbers contained in a
string to its numeric equivalent. it just is the opposite of the Str() function, however
it has some differences too.
Syntax:
Val(String)
Note: the string must be put in quotes
Working :-
if we feed it a value like this
Val(“124566”)
will produce the number 124566
and,
Val(“125.0066”)
will produce the number 125.0066
However, the Val function stops reading the string , when it encounters a character
which has NO NUMERIC EQUIVALENCE, which is for alphabets, symbols,
commas, etc are not recognized. But, Blanks , Tabs , and linefeed characters are
simply removed and not counted . included at all.
Val (“12534ABCD2345”)
gives output 12534. Why ? because when it reads “A” after four, it simply doesn’t
recognize it as a number, and the string is not read further, so even if there are
numbers at the back of the string, they aren’t added because they are never read.
now for more information, this function also recognizes the radix prefixes &O for
(Octal numbers) and &H for (Hexadecimal numbers) for example
Val("&H2D")
Dim N1 , N2 , N3 as Integer
N1 = Val(text1.text)
N2 = Val(text2.text)
N3 = N1*N2
text3.text = Str(N3)
now if you remove the “Val” functions , you will get a “Type mismatch” error in
runtime. however you will get the same error if you do not convert it back to string
when passing it to a string variable, but if you are directly passing it a textbox, then
the Str() function isn’t really necessary.
2. Sgn Function
This function is used to determine the sign of a number.
Syntax:
--------------
Sgn(number)
Note: the “number “ should be a valid number or numeric expression
If the number is
Positive ( number > 0 ) then sign value returned is “1”
Negative (number < 0 ) then sign value returned is “-1”
Zero (number = 0) then sign value returned is “0”
example:
Dim N1 , N2 , N3 As Integer
N1 = 234
N2 = -19
N3 = 0
Print Sgn(N1)
Print Sgn(N2)
Print Sgn(N3)
------------------
the output is
1
-1
0
--------------------------
3. Int and Fix Functions
Both these functions are used to remove the numbers left to the decimal point. but
these functions work differently.
num1 = Fix(134.2423)
num1 = Int(134.2423)
both Fix() and Int() functions behave the same for positive fractional numbers, but
in case of negative input number, they differ
num1 = Int(-2554.23)
num1 = Int(-2554.23)
4. Rnd Function
The Rnd() function is used to generate a random number. This function returns a
value equal or greater than zero , but lesser than 1.
Syntax:
--------------
Rnd([number])
if [Number] argument is :
2. Equal to Zero
- the Rnd() function generates the most recently generated number, which remains
the same
if you want integer numbers , simply put the Int() function in front of the expression
Int (( upperlimit – lowerlimit ) * Rnd + lowerlimit)
note that numbers generated WILL be BETWEEN the limits, as the limit numbers
are excluded.
Note :
before calling the Rnd() function , one should use the Randomize() statement
without an argument to initialize the random number generator based on the
system timer. But if you use the Randomize (number) , then the Randomize
Statement initializes the random number generator using the optional (number)
argument as the seed value.
some examples:
R_num = Int((10-4)*Rnd + 4)
Print R_num
---------------
the output will be (one of the output possibilities)
Also,
To repeat sequences of random numbers, call Rnd() with a negative argument
immediately before using Randomize with a numeric argument. using randomize
with the same value for number does not repeat the previous sequence.
-----------------------------------------------------------
5. Format Function
Format function rearranges number, strings, dates or any other value in a
“formatted” way. Mainly used to represent dates in different ways when needed.
syntax
-------------------
Format(expression,[Format],[firstdayofweek],[firstweekofyear])
where,
expression – Required. this is the value which is to be formatted
[format] – Optional. A valid names or user defined format expression.
[firstdayofweek] – Optional. A constant that specifies the first day of the week
[firstweekofyear] – Optional. A constant that specifies the first week of the year.
Symbol Use
“0” :- Digit placeholder; prints trailing or leading zeros into
positions.
“#” :- Digit placeholder; no trailing or leading zeros.
“.” :- Decimal placeholder.
(”) :- the “ is a thousands placeholder/
- + $ () space :- literal character characters are displayed
exactly as typed into the format string
these format options enable an user to display numbers in different ways when
needed ; one can specify the number of decimal places, leading or trailing zeros,
currency formats and others. [/CODE]
Number formatting
The following block will give information on how to format a number in several
ways.
Format(10203.192,”0000000.00000”)
Result :- 0010203.19200 // leading and trailing zeros
Format(10203.192, “#####.#####”)
Result :- 10203.192 // no leading or trailing zeros
Format(10203.192, “#####.00000”)
Result :- 10203.19200 // only trailing zeros
Format(10203.192, “##,###.00000”)
Result :- 10,203.19200 // thousands comma inserted in number
Format(10203.192, “$##,###.000”)
Result :- $10,203.192 // currency formatting
you can try as many combinations as possible, depending on what format you need
------------------
The symbol for decimal separator is a period (.) and the thousands separator is the
comma (,)
Also
Note: the conversions above are possible when the country in the Windows Control
Panel is set to [ English – (United States)].
------------------
Also the separator character depends on the selection of the country, for example
many countries will use the imperial system of numbers , and other will use the
metric system. in these cases the commas will be inserted according to the country
selected in Windows.
Format(Now, “m/d/yy”)
-Returns a format like this : [ (month)-(day)-(year) ] where the all values are in 2
digits.
example : (5-19-06)