Copyright 2011 Pearson Addison-Wesley
Addison Wesley
is an imprint of
2011 Pearson Addison-Wesley. All rights reserved.
Addison Wesley
is an imprint of
Section 3.3
Performing Calculations
Visual Basic has powerful arithmetic
operators that perform calculations
with numeric variables and literals.
Copyright 2011 Pearson Addison-Wesley
Common Arithmetic Operators
Visual Basic provides operators for the
common arithmetic operations:
+ Addition
- Subtraction
* ultiplication
/ !ivision
^ "#ponentiation
Chapter 3$ Slide %
Copyright 2011 Pearson Addison-Wesley
Common Arithmetic Operators
Addition
dblTotal = dblPrice + dblTax
Subtraction
dblNetPrice = dblPrice dblDiscount
ultiplication
intArea = intLength * intWidth
!ivision
dblAerage = intTotal / int!te"s
"#ponentiation
dbl#ube = dbl$ide ^ %
Chapter 3$ Slide 3
Copyright 2011 Pearson Addison-Wesley
Special &nteger !ivision Operator
'he bac(slash )&* is used as an integer division
operator
!ivides one integer b+ another
'he result is alwa+s an integer, created b+
discarding an+ remainder from the division
&f calculating the number of hours in a given
number of minutes
int'ours = int(inutes & )*
-ith intinutes e.ual to /01, this calculation
will result in the value 3 assigned to int2ours
Chapter 3$ Slide 3
Copyright 2011 Pearson Addison-Wesley
odulus )O!* Operator
'his operator can be used in place of the bac(slash
operator to give the remainder of a division operation
int+e"ainder = ,- (.D % / result is 0
dbl+e"ainder = ,-12 (.D % / result is 012
4se of the 5 or O!
operator to perform integer
division b+ 6ero causes a
!ivideB+7ero"#ception
runtime error
Chapter 3$ Slide 8
Copyright 2011 Pearson Addison-Wesley
"#plicit '+pe Conversions
A function performs some predetermined
operation and provides a single output
VB provides a set of functions that permit
narrowing conversions with Option Strict On
'hese functions will accept a constant,
variable name, or arithmetic e#pression
'he function returns the converted value
Chapter 3$ Slide 9
Copyright 2011 Pearson Addison-Wesley
"#plicit '+pe Conversion "#amples
:ounding can be done with the C&nt function
int#ount = #!nt3,0145 / int#ount alue is
,0
int#ount = #!nt3,0125 / int#ount alue is
,%
CStr converts an integer value to a string
Di" strText As $tring = #$tr30)5
C!ec converts a string to a double
Di" dblPa6 As Double = #Dbl378,92**75
C!ate converts a string to a date
Di" dat'ired As Date =
#Date3:0/,4/0*,07*
Chapter 3$ Slide ;
Copyright 2011 Pearson Addison-Wesley
Commonl+ 4sed Conversion
<unctions
2ere are some commonl+ used conversion
functions:
Chapter 3$ Slide =
<unction !escription
Cint )expression* Converts expression to an integer
Cdbl )expression* Converts expression to a double
Cdate )expression* Converts expression to a date
Cdec )expression* Converts expression to a decimal
Cstr )expression* Converts expression to a string
Copyright 2011 Pearson Addison-Wesley
A <ull >ist of Conversion <unctions
'here are conversion functions for each data t+pe:
#;ool 3 expression 5 #!nt 3 expression 5
#;6te 3 expression 5 #Lng 3 expression 5
##har 3 expression 5 #.b< 3 expression 5
#Date 3 expression 5 #$hort 3 expression 5
#Dbl 3 expression 5 #$ng 3 expression 5
#Dec 3 expression 5 #$tr 3 expression 5
Chapter 3$ Slide 0