Arithmetic in Visual Basic

Level:
Level1

Written By TheVBProgramer.

Most programs will involve some arithmetic computations. The VB arithmetic operators and their order of precedence are given in the table below. Precedence refers to what operations will be done first when a computation involves more than one operation. The rules in VB are the same as those in algebra. You may find it helpful to think of the phrase "Please Excuse My Dear Aunt Sally" for Parentheses / Exponentiation / Multiplication and Division (same precedence left-to-right) / Addition and Subtraction (same precedence left-to-right). Bear in mind that in VB, the precedence of "integer division" and "modulo" are below multiplication and "real" division, but above addition and subtraction.

 

 

 

 

Operation

Operator

Example

exponentiation

^

(this symbol is the "caret" - the shifted character above the number "6" on the keyboard)

intX = 2 ^ 3

(8 would be stored in intX because 23 = 8)

multiplication and "real" division (division where any digits to right of the decimal point are retained)

* for multiplication

/ for real division

(Multiplication and real division have equal precedence left-to-right)

intX = 4 * 3 / 2 * 5

(30 would be stored in intX)

integer division

\

intX = 15 \ 2

(7 would be stored in intX. With integer division, the portion of the result after the decimal point is lost. So even though the result is 7.5, the .5 is lost.)

modulo

Mod (the remainder of integer division)

intX = 15 Mod 2

(1 would be stored in intX. 15 divided by 2 is 7, with a remainder of 1. It is the remainder of 1 that is stored with Mod.)

addition and subtraction

+ for addition

- for subtraction

(Addition and subtraction have equal precedence left-to-right)

intX = 1 + 2 - 3 + 4

(4 would be stored in X.)

 

 

Bear in mind that parentheses can override the precedence of operations listed above, for example:

 

intX = 3 + 4 * 5

 

would cause 23 to be stored in intX (because you multiply first, then add).

 

 

But

 

intX = (3 + 4) * 5

 

would cause 35 to be stored in intX (because the parentheses caused the addition to take place first).

 

Parentheses in an expression can be nested (that is, you can have one set of parentheses within another). In the case of nested parentheses, the inner-most set is always evaluated first. For example, the statement

 

intX = (6 * (5 + 4) - 1) * 2

 

would cause 106 to be stored in intX. Can you see why?

 

Here are two other expressions for you to check out.

 

(1) The following code will cause 16 to be stored in the variable intY. Can you see why?

 

intX = 2

intY = 3 + 4 * intX ^ intX - 4 + (5 / (3 + 2))

 

 

(2) The following code will cause -61 to be stored in the variable intA. Can you see why?

 

intB = 3

intA = 4 * 5 / 5 \ 5 + 3 - intB * 2 - ((6 + 4) * 5 + 2 ^ intB)

 

 

Although the integer division (\) and MOD operators are probably used less frequently than the other arithmetic operators, they are useful for solving certain types of problems. Here are two examples:

 

(1) In date validation routines, it is often necessary to check whether or not a year is a leap year. The rule is that if a year is evenly divisible by four ("evenly divisible" means that the remainder is zero), it is a leap year. For example, years like 1992, 1996, and 2000 are leap years, while 1900, 1994, and 1997 are not. A test can be made as follows:

 

If (intYear Mod 4) = 0 Then ...

 

 

(2) Here's a bit of code that, given a total number of minutes, breaks down those total minutes into hours and minutes:

 

intHours = intTotMins \ 60

intMins = intTotMins Mod 60

 

If the variable intTotMins contained 285, then after the above code was executed, the variable intHours would contain 4 and the variable intMins would contain 45.

If you enjoyed this post, subscribe for updates (it's free)

conversion from MBPS into KBPS

Hi All..

i want to convert the value which is in mbps into the value in kbps. please help me in giving the code.

for ex: i want to convert 4MBPS into 4000KBPS.

How to convert MBPS value into KBPS value

Hi All..
Please do tell me how to convert value which is in MBPS into the value in KBPS in VB 6.0.

For eg: I want to convert 4MBIT/S into 4000 KBIT/S.

program

w.a.p to show one to ten digit in combo-box convert in words

sir can you help me with this

sir can you help me with this problem

write a program that takes a positive integer parameter and returns the number that result from adding up the digits of the parameter

for example

367

the program should print "16" (3+6+7)

make a date and time

hi sir,
i'm trying to make a date and time in my interface..
but i'm confuse how to make it..
i want the date and time will appear automatically when we run..
can you show me how to make it..?

hai.. sir

hi sir can you please give to me the code on how to execute mathematical operation in visual basic.. thank you.....

Multiplication Table

Hello, I need to create a multiplication table for numbers 1 - 9. Where x is the number entered by the user and y is the result when multiplied by any number 1 - 9. Help please on how to start this!

one question

i want to ask you..how to calculate with combo box in visual basic..help me please..

one question

i want to ask you..how to calculate with combo box in visual basic..help me please..

Thanks

ThaThanks Sir. Your Information was Very HelpFul To me Kindly Inform Me with Latest features in Vb

How do I show the outcome in a message box?

intX = (60*5)
convert.ToString(intX)
MsgBox = (intX "minutes remaining.", _..., _
"5 Hours to Minutes.")

subtracting text boxes

nandita ghosh = legend
got me out of a pickle =P
tyvm

VB6

Here is my code to calculate time difference. I wrote this programm to calculate the total time taken by process. the problem is that the numbers are getting converted to sceintific number which i don't want. how do i solve this problem.

Private Sub Command1_Click()
Dim result1 As Double
Dim result2 As Double
Dim finalresult As Double
Dim covertmin As Double
Dim convertsec As Double
Dim finalmin As String

result1 = Text1.Text * 60 + Text2.Text
result2 = Text3.Text * 60 + Text4.Text
finalresult = result2 - result1
covertmin = finalresult / 60

finalmin = Mid(CStr(covertmin), 1, InStr(1, CStr(covertmin), ".") - 1)

convertsec = finalresult Mod 60

MsgBox "min:" & finalmin & " sec:" & convertsec
End Sub

use Round() Function or

use Round() Function or Format() Function.

vpaf

How do I use text boxes to add numbers in VB6

I'm trying to duplicate a program I created in MS Access in VB6. I've figured out how to make a form and place text boxes in it, but how do I get the two textboxes to add up to another 3rd text box?
Please help!

please provide a few more

please provide a few more examples

VB

hi.. sir,

i wants newer topics, tutorials and coding and questions

Perhaps be more specific?

Doing that wont get you your request

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.