Lesson 18: Math Functions in VB2019
Master essential mathematical functions for complex calculations
Key Takeaway
VB2019 provides powerful built-in math functions that allow you to perform complex calculations without writing custom algorithms.
In this lesson, we'll explore the essential mathematical functions built into Visual Basic 2019. These functions allow you to perform advanced calculations, handle complex numerical operations, and solve mathematical problems efficiently. We'll cover functions ranging from basic absolute values to logarithmic and random number generation.
18.1 Mathematical Functions Overview
Visual Basic 2019 includes numerous built-in mathematical functions that belong to the Math
class. These functions provide ready-made solutions for common mathematical operations, saving you time and effort in coding complex algorithms.
Core Math Functions
Abs, Exp, Log, Sqrt and more
Random Numbers
Generate random values with Rnd
Value Conversion
Round, Fix, and Int for number formatting
Advanced Math
Trigonometric and financial functions
18.1(a) Function Reference Table
Here's a quick reference of the most commonly used math functions:
Function | Syntax | Description | Example |
---|---|---|---|
Abs | Math.Abs(number) | Returns absolute value | Math.Abs(-5.5) = 5.5 |
Exp | Math.Exp(number) | Returns e raised to a power | Math.Exp(1) ≈ 2.718 |
Fix | Fix(number) | Truncates decimal part | Fix(8.9) = 8 |
Int | Int(number) | Returns largest integer ≤ number | Int(8.9) = 8 |
Log | Math.Log(number) | Natural logarithm | Math.Log(10) ≈ 2.302 |
Rnd | vbMath.Rnd() | Random number [0,1) | vbMath.Rnd() → 0.734 |
Round | Math.Round(number, decimals) | Rounds to specified decimals | Math.Round(3.14159, 2) = 3.14 |
Sqrt | Math.Sqrt(number) | Square root | Math.Sqrt(25) = 5 |
18.2 Core Math Functions
Let's explore each mathematical function in detail with practical examples:
Returns the absolute value of a number, which is its value without regard to its sign.
Private Sub BtnAbs_Click(sender As Object, e As EventArgs) Handles BtnAbs.Click Dim num As Double = Val(TxtNumber.Text) LblResult.Text = Math.Abs(num).ToString() End Sub
Output:
Returns e (approximately 2.71828) raised to the specified power.
Private Sub BtnExp_Click(sender As Object, e As EventArgs) Handles BtnExp.Click Dim exponent As Double = Val(TxtExponent.Text) LblExpResult.Text = Math.Exp(exponent).ToString("F4") End Sub
Output:
Fix truncates the decimal part of a number. Int returns the largest integer less than or equal to the specified number.
Private Sub BtnFixInt_Click(sender As Object, e As EventArgs) Handles BtnFixInt.Click Dim num As Double = Val(TxtNumber.Text) LblFix.Text = Fix(num).ToString() LblInt.Text = Int(num).ToString() ' For negative numbers: Dim negNum As Double = -5.7 LblFixNeg.Text = Fix(negNum).ToString() ' Returns -5 LblIntNeg.Text = Int(negNum).ToString() ' Returns -6 End Sub
Output:
Returns the natural (base e) logarithm of a specified number.
Private Sub BtnLog_Click(sender As Object, e As EventArgs) Handles BtnLog.Click Dim num As Double = Val(TxtLogNumber.Text) If num > 0 Then LblLogResult.Text = Math.Log(num).ToString("F4") Else LblLogResult.Text = "Invalid input" End If End Sub
Output:
Generates a random number between 0.0 and 1.0. To generate integers in a specific range, combine with Int function.
Private Sub BtnRandom_Click(sender As Object, e As EventArgs) Handles BtnRandom.Click ' Generate random number between 0 and 1 Dim randomValue As Double = vbMath.Rnd() LblRandom1.Text = randomValue.ToString("F4") ' Generate random integer between 1 and 6 Dim diceRoll As Integer = Int(vbMath.Rnd() * 6) + 1 LblDice.Text = diceRoll.ToString() ' Generate random integer between 1 and 100 Dim randomInt As Integer = Int(vbMath.Rnd() * 100) + 1 LblRandom2.Text = randomInt.ToString() End Sub
Output:
Rounds a value to the nearest integer or to the specified number of fractional digits.
Private Sub BtnRound_Click(sender As Object, e As EventArgs) Handles BtnRound.Click Dim num As Double = Val(TxtRoundNumber.Text) Dim decimals As Integer = CInt(NumDecimals.Value) LblRoundResult.Text = Math.Round(num, decimals).ToString() End Sub
Output:
Returns the square root of a specified number.
Private Sub BtnSqrt_Click(sender As Object, e As EventArgs) Handles BtnSqrt.Click Dim num As Double = Val(TxtSqrtNumber.Text) If num >= 0 Then LblSqrtResult.Text = Math.Sqrt(num).ToString("F4") Else LblSqrtResult.Text = "Invalid input" End If End Sub
Output:
Lesson Summary
In this lesson, you've learned how to leverage VB2019's built-in mathematical functions to perform complex calculations efficiently:
Core Functions
Mastered Abs, Exp, Log, Sqrt for fundamental mathematical operations
Value Conversion
Learned to use Fix, Int, and Round for number formatting and conversion
Random Numbers
Implemented Rnd function for generating random values
Practical Applications
Applied math functions to solve real-world programming problems
These mathematical functions form the foundation for more advanced operations in programming. By understanding and applying these functions, you can create applications that perform complex calculations efficiently. In the next lesson, we'll explore trigonometric functions for geometric calculations.
Next Lesson
Ready to learn about trigonometric functions? Continue to Lesson 19: Trigonometric Functions.
Related Resources

Visual Basic 2019 Made Easy
Unlock the power of Visual Basic 2019 with this comprehensive, easy-to-follow handbook written by Dr. Liew, renowned educator and founder of the popular programming tutorial website VBtutor.net. Whether you're new to programming or brushing up your skills, this book is your perfect companion to learn Visual Basic 2019 from the ground up.
What You'll Learn:
- Understand Core Programming Concepts: Grasp the foundational principles of Visual Basic 2019, including variables, data types, conditional logic, loops, and event-driven programming.
- Develop Real Windows Desktop Applications: Build fully functional and interactive Windows apps using Visual Studio 2019—guided through step-by-step tutorials.
- Apply Dozens of Ready-to-Use Examples: Explore a rich collection of practical sample programs, from basic calculators to image viewers and database applications.
- Adapt and Reuse Code for Your Own Projects: Customize professionally written code snippets to speed up your development process and bring your ideas to life.
- Package and Deploy Like a Pro: Learn how to compile, test, and distribute your Visual Basic applications seamlessly with built-in deployment tools.

Visual Basic Programming With Code Examples
Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.
What You'll Learn:
- Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
- Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
- 48 Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
- Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.