Tools - Math Utility Class
cToolsMath - Math Calculation Tools
Overview
Provides mathematical operations such as rounding, random numbers, and rounding to nearest.
Enumeration
EnumRoundingMode
vb
Public Enum EnumRoundingMode
Rounding = 0 ' Round to nearest
Upper = 1 ' Round up (ceiling)
Downer = 2 ' Round down (floor)
End EnumMethods
Ceil
Rounds up (ceiling).
vb
Public Function Ceil( _
ByVal Num As Variant, _
Optional ByVal Dot As Long _
) As CurrencyParameters:
| Parameter | Type | Description |
|---|---|---|
Num | Variant | Number to round |
Dot | Long | Decimal places to keep (default 0) |
Example:
vb
Debug.Print VBMAN.ToolsMath.Ceil(3.14) ' Output: 4
Debug.Print VBMAN.ToolsMath.Ceil(3.14, 1) ' Output: 3.2
Debug.Print VBMAN.ToolsMath.Ceil(-3.14) ' Output: -3GetRandRange
Gets random integer in specified range (inclusive).
vb
Public Function GetRandRange(a As Long, b As Long) As LongExample:
vb
' Random integer between 1-100
Dim Rand As Long
Rand = VBMAN.ToolsMath.GetRandRange(1, 100)
Debug.Print Rand
' Random boolean
Dim IsTrue As Boolean
IsTrue = (VBMAN.ToolsMath.GetRandRange(0, 1) = 1)RoundCurrency
Rounds to nearest (supports multiple rounding modes).
vb
Public Function RoundCurrency( _
ByVal Value As Variant, _
Optional ByVal DecimalPlaces As Integer = 0, _
Optional ByVal RoundingMode As EnumRoundingMode = 0 _
) As CurrencyParameters:
| Parameter | Type | Description |
|---|---|---|
Value | Variant | Number to round |
DecimalPlaces | Integer | Decimal places (default 0, range -4~4) |
RoundingMode | EnumRoundingMode | Rounding mode |
Example:
vb
' Round to nearest
Debug.Print VBMAN.ToolsMath.RoundCurrency(3.14159, 2) ' 3.14
Debug.Print VBMAN.ToolsMath.RoundCurrency(3.14159, 2, Rounding) ' 3.14
' Round up (ceiling)
Debug.Print VBMAN.ToolsMath.RoundCurrency(3.14159, 2, Upper) ' 3.15
' Round down (floor)
Debug.Print VBMAN.ToolsMath.RoundCurrency(3.14159, 2, Downer) ' 3.14
' Negative decimal places (round to tens)
Debug.Print VBMAN.ToolsMath.RoundCurrency(1234, -2, Rounding) ' 1200Complete Example
vb
Private Sub MathDemo()
Dim Price As Currency
Price = 123.456
' Round product price to cents
Debug.Print "Standard price: " & VBMAN.ToolsMath.RoundCurrency(Price, 2)
' Discount price rounded up (no loss)
Debug.Print "Discount price: " & VBMAN.ToolsMath.RoundCurrency(Price * 0.95, 2, Upper)
' Generate random verification code
Dim Code As String
Code = Format(VBMAN.ToolsMath.GetRandRange(1000, 9999), "0000")
Debug.Print "Verification code: " & Code
End Sub