Skip to content

VBMAN.ToolsMath - Mathematical Operation Tool Object

Overview

VBMAN.ToolsMath provides common mathematical operation functions, including high-precision rounding, integer conversion, random number generation, etc., especially suitable for financial calculations and precise numerical processing.

Core Features

  • High-precision Calculation: Uses Currency type, supports 4 decimal precision
  • Multiple Rounding Modes: Round, round up, round down
  • Large Number Support: Can handle calculations within large ranges
  • Type Safety: Strong type parameters, avoiding implicit conversion errors

Enums

EnumRoundingMode

Rounding mode enum

vb
Public Enum EnumRoundingMode
    Rounding = 0    ' Traditional rounding
    Upper = 1       ' Round up
    Downer = 2      ' Round down
End Enum

Methods

Ceil

Truncate rounding (towards positive infinity)

vb
Public Function Ceil(ByVal Num As Variant, Optional ByVal Dot As Long) As Currency

Parameters:

  • Num - Number to round
  • Dot - Decimal precision (default 0, i.e., integer)

Example:

vb
' Basic rounding
Dim r1 As Currency
r1 = VBMAN.ToolsMath.Ceil(3.7)      ' Result: 4

Dim r2 As Currency
r2 = VBMAN.ToolsMath.Ceil(-3.7)     ' Result: -3

' Keep 2 decimal places
Dim r3 As Currency
r3 = VBMAN.ToolsMath.Ceil(3.14159, 2)   ' Result: 3.15

' Keep 1 decimal place
Dim r4 As Currency
r4 = VBMAN.ToolsMath.Ceil(10.99, 1)     ' Result: 11.0

RoundCurrency

High-precision rounding

vb
Public Function RoundCurrency(ByVal Value As Variant, Optional ByVal DecimalPlaces As Integer = 0, Optional ByVal RoundingMode As EnumRoundingMode = 0) As Currency

Parameters:

  • Value - Number to round
  • DecimalPlaces - Decimal places (-4 to 4, default 0)
  • RoundingMode - Rounding mode (default traditional rounding)

Example:

vb
' Basic rounding
Dim r1 As Currency
r1 = VBMAN.ToolsMath.RoundCurrency(3.5)      ' Result: 4

Dim r2 As Currency
r2 = VBMAN.ToolsMath.RoundCurrency(3.4)      ' Result: 3

' Keep decimal places
Dim r3 As Currency
r3 = VBMAN.ToolsMath.RoundCurrency(3.14159, 2)   ' Result: 3.14

Dim r4 As Currency
r4 = VBMAN.ToolsMath.RoundCurrency(3.14159, 3)   ' Result: 3.142

' Negative handling
Dim r5 As Currency
r5 = VBMAN.ToolsMath.RoundCurrency(-3.5)     ' Result: -4 (away from zero)

' Different rounding modes
Dim r6 As Currency
r6 = VBMAN.ToolsMath.RoundCurrency(3.1, 0, Upper)    ' Result: 4 (round up)

Dim r7 As Currency
r7 = VBMAN.ToolsMath.RoundCurrency(3.9, 0, Downer)   ' Result: 3 (round down)

' Financial calculation (keep 2 decimal places)
Dim price As Currency
Dim quantity As Integer
Dim total As Currency

price = 19.99
quantity = 3
total = VBMAN.ToolsMath.RoundCurrency(price * quantity, 2)
' Result: 59.97

GetRandRange

Get random integer within specified range

vb
Public Function GetRandRange(a As Long, b As Long) As Long

Parameters:

  • a - Range start value (inclusive)
  • b - Range end value (inclusive)

Returns: Random integer in [a, b] range

Example:

vb
' Dice roll (1-6)
Dim dice As Long
dice = VBMAN.ToolsMath.GetRandRange(1, 6)
Debug.Print "Dice roll: " & dice

' Random verification code (1000-9999)
Dim code As Long
code = VBMAN.ToolsMath.GetRandRange(1000, 9999)
Debug.Print "Verification code: " & code

' Array random index
Dim arr(1 To 10) As String
Dim randomIndex As Long
randomIndex = VBMAN.ToolsMath.GetRandRange(LBound(arr), UBound(arr))
Debug.Print "Randomly selected: " & arr(randomIndex)

' Random color
Dim r As Long, g As Long, b As Long
r = VBMAN.ToolsMath.GetRandRange(0, 255)
g = VBMAN.ToolsMath.GetRandRange(0, 255)
b = VBMAN.ToolsMath.GetRandRange(0, 255)
Me.BackColor = RGB(r, g, b)

Comprehensive Examples

Example 1: Financial Amount Calculation

vb
Private Sub CalculateInvoice()
    Dim items(1 To 3, 1 To 3) As Currency
    ' Product: Unit Price, Quantity, Discount
    items(1, 1) = 199.99: items(1, 2) = 2: items(1, 3) = 0.95  ' 95% discount
    items(2, 1) = 59.5:   items(2, 2) = 5: items(2, 3) = 1     ' No discount
    items(3, 1) = 299:    items(3, 2) = 1: items(3, 3) = 0.8   ' 20% discount
    
    Dim subtotal As Currency
    Dim i As Integer
    
    For i = 1 To 3
        Dim itemTotal As Currency
        itemTotal = items(i, 1) * items(i, 2) * items(i, 3)
        ' Each product amount keeps 2 decimal places
        itemTotal = VBMAN.ToolsMath.RoundCurrency(itemTotal, 2)
        subtotal = subtotal + itemTotal
        Debug.Print "Product" & i & ": " & itemTotal
    Next i
    
    Dim tax As Currency
    tax = VBMAN.ToolsMath.RoundCurrency(subtotal * 0.13, 2)  ' 13% tax
    
    Dim total As Currency
    total = VBMAN.ToolsMath.RoundCurrency(subtotal + tax, 2)
    
    Debug.Print "Subtotal: " & subtotal
    Debug.Print "Tax: " & tax
    Debug.Print "Total: " & total
End Sub

Example 2: Price Display Formatting

vb
Private Function FormatPrice(price As Currency, decimals As Integer) As String
    Dim rounded As Currency
    rounded = VBMAN.ToolsMath.RoundCurrency(price, decimals)
    FormatPrice = Format(rounded, "0." & String(decimals, "0"))
End Function

Private Sub TestFormatting()
    Debug.Print FormatPrice(19.999, 2)   ' 20.00
    Debug.Print FormatPrice(19.994, 2)   ' 19.99
    Debug.Print FormatPrice(19.995, 2)   ' 20.00
End Sub

Example 3: Lottery System

vb
Private Sub LotteryDraw()
    ' Prize pool
    Dim prizes As Variant
    prizes = Array("First Prize: iPhone", "Second Prize: iPad", "Third Prize: Headphones", "Thanks for participating")
    
    ' Winning probabilities (cumulative)
    Dim probabilities As Variant
    probabilities = Array(5, 15, 40, 100)  ' 5%, 10%, 25%, 60%
    
    ' Random number 1-100
    Dim rand As Long
    rand = VBMAN.ToolsMath.GetRandRange(1, 100)
    
    Dim result As String
    Dim i As Integer
    
    For i = 0 To UBound(probabilities)
        If rand <= probabilities(i) Then
            result = prizes(i)
            Exit For
        End If
    Next i
    
    MsgBox "Random number: " & rand & vbCrLf & "Result: " & result
End Sub

Example 4: Pagination Calculation

vb
Private Function CalculatePagination(totalRecords As Long, pageSize As Long, currentPage As Long) As Scripting.Dictionary
    Dim result As New Scripting.Dictionary
    
    ' Calculate total pages (round up)
    Dim totalPages As Long
    totalPages = VBMAN.ToolsMath.Ceil(totalRecords / pageSize)
    
    ' Ensure current page is valid
    If currentPage < 1 Then currentPage = 1
    If currentPage > totalPages Then currentPage = totalPages
    
    ' Calculate start and end records
    Dim startRecord As Long
    Dim endRecord As Long
    startRecord = (currentPage - 1) * pageSize + 1
    endRecord = currentPage * pageSize
    If endRecord > totalRecords Then endRecord = totalRecords
    
    result("TotalRecords") = totalRecords
    result("TotalPages") = totalPages
    result("CurrentPage") = currentPage
    result("PageSize") = pageSize
    result("StartRecord") = startRecord
    result("EndRecord") = endRecord
    
    Set CalculatePagination = result
End Function

Best Practices

  1. Use Currency for Financial Calculations: Avoid Double type precision issues
  2. Clear Rounding Mode: Choose appropriate rounding mode based on business requirements
  3. Set Random Seed: Use Randomize to initialize random number generator
  4. Range Check: Check range boundaries before using random numbers
  5. Formatting Display: Use Format function for display formatting after calculation

Notes

  • RoundCurrency decimal places limited to -4 to 4
  • Very large numbers may cause overflow, Currency maximum supports 922337203685477.5807
  • GetRandRange needs to call Randomize first to get different sequences
  • Negative number rounding rounds away from zero

VB6 and LOGO copyright of Microsoft Corporation