0% found this document useful (0 votes)
3K views

Visual Basic 6 Library Functions

The document discusses numeric functions in the Visual Basic library. It covers functions like Val(), which converts strings to numbers, Sgn() to determine the sign of a number, Int() and Fix() to remove decimals, Rnd() to generate random numbers, and Format() to format numbers, dates, and times in different ways using format symbols. Examples are provided to demonstrate the usage of each function.

Uploaded by

bulkSoft
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

Visual Basic 6 Library Functions

The document discusses numeric functions in the Visual Basic library. It covers functions like Val(), which converts strings to numbers, Sgn() to determine the sign of a number, Int() and Fix() to remove decimals, Rnd() to generate random numbers, and Format() to format numbers, dates, and times in different ways using format symbols. Examples are provided to demonstrate the usage of each function.

Uploaded by

bulkSoft
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Visual Basic 6 Library Functions – Numeric Type

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.

so therefore we get something like this

Val (“4566 3442 5553 “)


gives the output 456634425553 , no tabs , spaces included.

Now, for input something like this it has another output

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")

returns its decimal equivalent, which is 45.


the main use of Val function is to obtain numeric values from string variables, such
as values inputted / obtained from a text box.

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

now there are three cases for a sign of a number

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.

the Fix() function simply removes (truncates) the fractional part.

num1 = Fix(134.2423)

‘// num1 is stored with the value 134


and the Int() function removes the fractional part, and rounds down to the nearest
integer value

num1 = Int(134.2423)

‘// num1 is stored with the value 134

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 is stored with the value -2554

But the Int() function:

num1 = Int(-2554.23)

‘// num1 is stored with the value -2555


why ? because Int() function removes the fractional part, and rounds down to the
nearest integer value as known , so it is doing what it is supposed to do, as “-2555” is
smaller than “-2554”.
-------------------------------------------------

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])

Note: Here [number] denotes that it is optional argument


however the output is quite unpredictable, but it can be somewhat known on the
basis of what input has been given.

if [Number] argument is :

1. Less than Zero


- the Rnd() function generates the same number every time using [number] as an
initial value used to generate pseudorandom numbers (called Seed)

2. Equal to Zero
- the Rnd() function generates the most recently generated number, which remains
the same

3. Greater than Zero


- the Rnd() function generates the next random number in the sequence.

4. Argument Not Supplied


- the Rnd() function generates the next random number in the sequence.

now we can modify the use in several ways

to generate random numbers within a limit we can write it as


---------------------------
Syntax
(( upperlimit – lowerlimit ) * Rnd + lowerlimit)
----------------------------
where upperlimit and lowerlimit are the range numbers in which random numbers
have to be generated.

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:

Dim R_num As Integer

R_num = ((10 – 4)*Rnd +4)


Print R_num

R_num = Int((10-4)*Rnd + 4)
Print R_num
---------------
the output will be (one of the output possibilities)

9.171213 generated due to the Rnd() function with limits⇓


8 same as before, but Int() function removes the decimal
parts.⇓

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.

The expression argument a number to convert and he format argument is a string


made up of symbols that shows how to format the number. the most commonly used
symbols are listed :

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.

Date and Time Format


Formatting dates and times is pretty useful when you need to display it in a format
which isn’t in your data-type form. so the Format() function can be used to convert
a date, time to another format, using symbols representing date and time. The
following block will illustrate how to format dates in various forms.

Formatting Dates and Times

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)

Format (Now, “dddd,mmmm,dd,yyyy”)


-Returns a format like this : [ (day of week)-(month in words)-(day)-(year) ] where
the month is in alphabets , day is in numbers , year is in four digit number.
example : (Thursday , April, 15 , 2006)
Format (Now, “d-mmmm”)
-Returns a format like this : [ (day)-(month in words) ] where the day is in digits ,
and month is in words.
example : (15 April)

Format (Now, “d-mmm-yy”)


-Returns a format like this : [ (day)-(month in words)-(year) ] where days , years are
in numbers ( as discussed) and month is in 3 letter words.
example : (15-APR-06)

Format (Now, “hh:mm AM/PM”)


-Returns a format like this : [ (hour of day)-(minute of hour)] where , hour of day is
in AM./PM mode as AM/PM has been written in the format option.
example : (08:30 PM)
- if you do not specify this , it will come in a format (20:30)

Format (Now, “h:mm:ss A/P”)


-Same as above, but with a little difference, here instead of AM / PM; A , P will
appear
example : (08:30 P)

Format (Now, “d-mmmm h:mm AM/PM”)


-Returns a format like this : [ (day)-(month in words)-(hours)-(minutes)-(AM/PM) ]
example : (15 April 8:30 PM)
----------------------------------------
Of course there are innumerable permutations and combinations on how to display
a date, this formatting options are extremely useful on an “international scale” as
dates / times are represented in different ways in different countries.

You might also like