In Visual Basic, you can use all of the normal mathematical symbols to perform mathematical functions. For example:
- + - Used for addition, like 1+1=2.
- - - Used for subtraction, like 5-3=2.
- * - Used for multiplication, like 3*2=6
- / - Used for division, like 5/2=2.5
- MOD - This one is a bit stranger. This gets the remainder after an integer division. For example, 7 MOD 3 = 1, or 19 MOD 5 = 4.
NOTE: When using the ampersand (&) to concatenate two variables, VB will automatically attempt to convert both variables to strings.
When doing mathematical equations, VB has a few shortcuts. For example, rather than writing:
intVariable = intVariable + 10
We can simply write:
intVariable += 10
Other math operations include:
- Math.Sqrt(number) - Returns the square root of the number within the parentheses.
- Math.Round(number to round, number of decimal places) - Rounds the number to the specified number of decimal places.
- Math.Abs(number) - Returns the absolute value of the number within the parentheses.
- Math.Pi - Returns the number Pi.
There are also ways to convert strings to integers, or integers to strings. We will discuss conversions on the next page.
Previous
Next