vb6parse 1.0.1

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="VB6Parse Library Reference - round - Math">
    <title>round - Math - VB6Parse Library Reference</title>
    <link rel="stylesheet" href="../../../assets/css/style.css">
    <link rel="stylesheet" href="../../../assets/css/docs-style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
    <script src="../../../assets/js/theme-switcher.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
    <script>hljs.highlightAll();</script>
</head>
<body>
    <header class="docs-header">
        <div class="container">
            <h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/math/index.html">Math</a> / round</h1>
            <p class="tagline">VB6 Library Reference</p>
        </div>
    </header>

    <nav class="docs-nav">
        <div class="container">
            <a href="../../../index.html">Home</a>
            <a href="../../../library/index.html">Library Reference</a>
            <a href="../../../documentation.html">Documentation</a>
            <a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
            <a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
            <button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
                <span class="theme-icon">🌙</span>
            </button>
        </div>
    </nav>

    <main class="container">
        
        <article class="library-item">
            <h1 id="round-function">Round Function</h1>
<p>Returns a number rounded to a specified number of decimal places.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Round(expression, [numdecimalplaces])</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>expression</code> - Required. Numeric expression being rounded.</li>
<li><code>numdecimalplaces</code> - Optional. Number indicating how many places to the right of the decimal are included in the rounding. If omitted, integers are returned.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a value of the same type as <code>expression</code> that has been rounded to the specified number of decimal places.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>Round</code> function rounds numbers using "banker's rounding" (round to even), also known as "round half to even". This differs from the typical "round half up" method taught in schools.
<strong>Important Notes</strong>:
- Uses banker's rounding (round to nearest even number when exactly halfway)
- If <code>numdecimalplaces</code> is omitted, returns an integer
- If <code>numdecimalplaces</code> is 0, rounds to nearest integer
- If <code>numdecimalplaces</code> is positive, rounds to that many decimal places
- If <code>numdecimalplaces</code> is negative, rounds to the left of decimal point
- Returns same data type as input expression
<strong>Banker's Rounding Examples</strong>:
- Round(2.5) = 2 (rounds to even)
- Round(3.5) = 4 (rounds to even)
- Round(4.5) = 4 (rounds to even)
- Round(5.5) = 6 (rounds to even)
<strong>Rounding Behavior by numdecimalplaces</strong>:</p>
<table>
<thead>
<tr>
<th>numdecimalplaces</th>
<th>Effect</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr>
<td>Omitted</td>
<td>Round to integer</td>
<td>Round(2.7) = 3</td>
</tr>
<tr>
<td>0</td>
<td>Round to integer</td>
<td>Round(2.7, 0) = 3</td>
</tr>
<tr>
<td>Positive (e.g., 2)</td>
<td>Round to N decimals</td>
<td>Round(2.748, 2) = 2.75</td>
</tr>
<tr>
<td>Negative (e.g., -1)</td>
<td>Round to left of decimal</td>
<td>Round(2748, -1) = 2750</td>
</tr>
</tbody>
</table>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Financial Calculations</strong>: Round currency values to 2 decimal places</li>
<li><strong>Display Formatting</strong>: Round numbers for user display</li>
<li><strong>Statistical Analysis</strong>: Round computed values to significant digits</li>
<li><strong>Data Normalization</strong>: Standardize precision across datasets</li>
<li><strong>Measurement Values</strong>: Round sensor readings to appropriate precision</li>
<li><strong>Grade Calculation</strong>: Round student scores to whole numbers</li>
<li><strong>Percentage Display</strong>: Round percentages to desired precision</li>
<li><strong>Scientific Notation</strong>: Round to significant figures</li>
</ol>
<h2 id="basic-examples">Basic Examples</h2>
<h3 id="example-1-round-to-integer">Example 1: Round to Integer</h3>
<pre><code class="language-vbnet">Dim value As Double
Dim rounded As Integer
value = 3.7
rounded = Round(value)  &#x27; Returns 4</code></pre>
<h3 id="example-2-round-currency">Example 2: Round Currency</h3>
<pre><code class="language-vbnet">Dim price As Double
Dim roundedPrice As Double
price = 12.3456
roundedPrice = Round(price, 2)  &#x27; Returns 12.35</code></pre>
<h3 id="example-3-bankers-rounding">Example 3: Banker's Rounding</h3>
<pre><code class="language-vbnet">&#x27; Demonstrates round-to-even behavior
Dim result1 As Integer
Dim result2 As Integer
result1 = Round(2.5)  &#x27; Returns 2 (rounds to even)
result2 = Round(3.5)  &#x27; Returns 4 (rounds to even)</code></pre>
<h3 id="example-4-round-to-tens">Example 4: Round to Tens</h3>
<pre><code class="language-vbnet">Dim value As Long
Dim roundedToTens As Long
value = 2748
roundedToTens = Round(value, -1)  &#x27; Returns 2750</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="pattern-1-roundcurrency">Pattern 1: <code>RoundCurrency</code></h3>
<pre><code class="language-vbnet">Function RoundCurrency(amount As Double) As Double
    &#x27; Round to 2 decimal places for currency
    RoundCurrency = Round(amount, 2)
End Function</code></pre>
<h3 id="pattern-2-roundpercentage">Pattern 2: <code>RoundPercentage</code></h3>
<pre><code class="language-vbnet">Function RoundPercentage(percentage As Double, _
                        Optional decimals As Integer = 1) As Double
    &#x27; Round percentage to specified decimal places
    RoundPercentage = Round(percentage, decimals)
End Function</code></pre>
<h3 id="pattern-3-roundtosignificantfigures">Pattern 3: <code>RoundToSignificantFigures</code></h3>
<pre><code class="language-vbnet">Function RoundToSignificantFigures(value As Double, _
                                  sigFigs As Integer) As Double
    &#x27; Round to specified number of significant figures
    Dim magnitude As Double
    Dim factor As Double
    If value = 0 Then
        RoundToSignificantFigures = 0
        Exit Function
    End If
    magnitude = Int(Log(Abs(value)) / Log(10))
    factor = 10 ^ (sigFigs - magnitude - 1)
    RoundToSignificantFigures = Round(value * factor) / factor
End Function</code></pre>
<h3 id="pattern-4-roundupalways">Pattern 4: <code>RoundUpAlways</code></h3>
<pre><code class="language-vbnet">Function RoundUpAlways(value As Double, decimals As Integer) As Double
    &#x27; Always round up (ceiling behavior)
    Dim factor As Double
    factor = 10 ^ decimals
    If value &gt; 0 Then
        RoundUpAlways = Int(value * factor + 0.9999999) / factor
    Else
        RoundUpAlways = Int(value * factor) / factor
    End If
End Function</code></pre>
<h3 id="pattern-5-rounddownalways">Pattern 5: <code>RoundDownAlways</code></h3>
<pre><code class="language-vbnet">Function RoundDownAlways(value As Double, decimals As Integer) As Double
    &#x27; Always round down (floor behavior)
    Dim factor As Double
    factor = 10 ^ decimals
    If value &gt; 0 Then
        RoundDownAlways = Int(value * factor) / factor
    Else
        RoundDownAlways = Int(value * factor - 0.9999999) / factor
    End If
End Function</code></pre>
<h3 id="pattern-6-roundtonearest">Pattern 6: <code>RoundToNearest</code></h3>
<pre><code class="language-vbnet">Function RoundToNearest(value As Double, nearest As Double) As Double
    &#x27; Round to nearest multiple of a number
    &#x27; e.g., RoundToNearest(47, 5) = 45
    RoundToNearest = Round(value / nearest) * nearest
End Function</code></pre>
<h3 id="pattern-7-roundarray">Pattern 7: <code>RoundArray</code></h3>
<pre><code class="language-vbnet">Sub RoundArray(arr() As Double, decimals As Integer)
    &#x27; Round all elements in an array
    Dim i As Integer
    For i = LBound(arr) To UBound(arr)
        arr(i) = Round(arr(i), decimals)
    Next i
End Sub</code></pre>
<h3 id="pattern-8-roundifneeded">Pattern 8: <code>RoundIfNeeded</code></h3>
<pre><code class="language-vbnet">Function RoundIfNeeded(value As Double, decimals As Integer, _
                      threshold As Double) As Double
    &#x27; Only round if difference from rounded value exceeds threshold
    Dim rounded As Double
    rounded = Round(value, decimals)
    If Abs(value - rounded) &gt; threshold Then
        RoundIfNeeded = rounded
    Else
        RoundIfNeeded = value
    End If
End Function</code></pre>
<h3 id="pattern-9-roundfordisplay">Pattern 9: <code>RoundForDisplay</code></h3>
<pre><code class="language-vbnet">Function RoundForDisplay(value As Double) As String
    &#x27; Round and format for display based on magnitude
    If Abs(value) &lt; 0.01 Then
        RoundForDisplay = Format(Round(value, 4), &quot;0.0000&quot;)
    ElseIf Abs(value) &lt; 1 Then
        RoundForDisplay = Format(Round(value, 3), &quot;0.000&quot;)
    ElseIf Abs(value) &lt; 100 Then
        RoundForDisplay = Format(Round(value, 2), &quot;0.00&quot;)
    Else
        RoundForDisplay = Format(Round(value, 0), &quot;0&quot;)
    End If
End Function</code></pre>
<h3 id="pattern-10-symmetricround">Pattern 10: <code>SymmetricRound</code></h3>
<pre><code class="language-vbnet">Function SymmetricRound(value As Double, decimals As Integer) As Double
    &#x27; Traditional &quot;round half up&quot; instead of banker&#x27;s rounding
    Dim factor As Double
    Dim shifted As Double
    factor = 10 ^ decimals
    shifted = value * factor
    If shifted &gt;= 0 Then
        SymmetricRound = Int(shifted + 0.5) / factor
    Else
        SymmetricRound = Int(shifted - 0.5) / factor
    End If
End Function</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="example-1-financial-calculator">Example 1: Financial Calculator</h3>
<pre><code class="language-vbnet">&#x27; Precise financial calculations with proper rounding
Class FinancialCalculator
    Private m_precision As Integer
    Public Sub Initialize(Optional precision As Integer = 2)
        m_precision = precision
    End Sub
    Public Function CalculateInterest(principal As Double, _
                                     rate As Double, _
                                     periods As Integer) As Double
        &#x27; Calculate simple interest with rounding
        Dim interest As Double
        interest = principal * rate * periods
        CalculateInterest = Round(interest, m_precision)
    End Function
    Public Function CalculatePayment(loanAmount As Double, _
                                    interestRate As Double, _
                                    numPayments As Integer) As Double
        &#x27; Calculate loan payment with rounding
        Dim monthlyRate As Double
        Dim payment As Double
        monthlyRate = interestRate / 12
        If monthlyRate = 0 Then
            payment = loanAmount / numPayments
        Else
            payment = loanAmount * (monthlyRate * (1 + monthlyRate) ^ numPayments) / _
                     ((1 + monthlyRate) ^ numPayments - 1)
        End If
        CalculatePayment = Round(payment, m_precision)
    End Function
    Public Function CalculateTax(amount As Double, taxRate As Double) As Double
        &#x27; Calculate tax with rounding
        Dim tax As Double
        tax = amount * taxRate
        CalculateTax = Round(tax, m_precision)
    End Function
    Public Function CalculateTotal(subtotal As Double, taxRate As Double) As Double
        &#x27; Calculate total with tax
        Dim tax As Double
        Dim total As Double
        tax = Round(subtotal * taxRate, m_precision)
        total = subtotal + tax
        CalculateTotal = Round(total, m_precision)
    End Function
    Public Function SplitAmount(totalAmount As Double, _
                               numSplits As Integer) As Double()
        &#x27; Split amount evenly with proper rounding
        Dim splits() As Double
        Dim baseAmount As Double
        Dim remainder As Double
        Dim i As Integer
        ReDim splits(1 To numSplits)
        baseAmount = Round(totalAmount / numSplits, m_precision)
        For i = 1 To numSplits
            splits(i) = baseAmount
        Next i
        &#x27; Adjust for rounding errors
        remainder = Round(totalAmount - (baseAmount * numSplits), m_precision)
        splits(1) = Round(splits(1) + remainder, m_precision)
        SplitAmount = splits
    End Function
    Public Sub SetPrecision(precision As Integer)
        m_precision = precision
    End Sub
    Public Function GetPrecision() As Integer
        GetPrecision = m_precision
    End Function
End Class</code></pre>
<h3 id="example-2-statistical-rounder">Example 2: Statistical Rounder</h3>
<pre><code class="language-vbnet">&#x27; Round statistical values to appropriate precision
Module StatisticalRounder
    Public Function RoundMean(values() As Double, decimals As Integer) As Double
        &#x27; Calculate and round mean
        Dim sum As Double
        Dim i As Integer
        sum = 0
        For i = LBound(values) To UBound(values)
            sum = sum + values(i)
        Next i
        RoundMean = Round(sum / (UBound(values) - LBound(values) + 1), decimals)
    End Function
    Public Function RoundStdDev(values() As Double, decimals As Integer) As Double
        &#x27; Calculate and round standard deviation
        Dim mean As Double
        Dim sumSquaredDiff As Double
        Dim i As Integer
        Dim n As Integer
        n = UBound(values) - LBound(values) + 1
        mean = RoundMean(values, decimals + 2)
        sumSquaredDiff = 0
        For i = LBound(values) To UBound(values)
            sumSquaredDiff = sumSquaredDiff + (values(i) - mean) ^ 2
        Next i
        RoundStdDev = Round(Sqr(sumSquaredDiff / (n - 1)), decimals)
    End Function
    Public Function RoundPercentile(values() As Double, percentile As Double, _
                                   decimals As Integer) As Double
        &#x27; Calculate and round percentile
        Dim sortedValues() As Double
        Dim index As Double
        Dim lowerIndex As Integer
        Dim upperIndex As Integer
        Dim weight As Double
        Dim result As Double
        &#x27; Copy and sort array (simplified - would need sorting implementation)
        sortedValues = values
        index = percentile * (UBound(sortedValues) - LBound(sortedValues))
        lowerIndex = Int(index) + LBound(sortedValues)
        upperIndex = lowerIndex + 1
        weight = index - Int(index)
        If upperIndex &gt; UBound(sortedValues) Then
            result = sortedValues(lowerIndex)
        Else
            result = sortedValues(lowerIndex) * (1 - weight) + _
                    sortedValues(upperIndex) * weight
        End If
        RoundPercentile = Round(result, decimals)
    End Function
    Public Function RoundCorrelation(values1() As Double, values2() As Double, _
                                    decimals As Integer) As Double
        &#x27; Calculate and round correlation coefficient
        Dim mean1 As Double, mean2 As Double
        Dim sum As Double
        Dim sum1Sq As Double, sum2Sq As Double
        Dim i As Integer
        Dim correlation As Double
        mean1 = RoundMean(values1, decimals + 2)
        mean2 = RoundMean(values2, decimals + 2)
        sum = 0
        sum1Sq = 0
        sum2Sq = 0
        For i = LBound(values1) To UBound(values1)
            sum = sum + (values1(i) - mean1) * (values2(i) - mean2)
            sum1Sq = sum1Sq + (values1(i) - mean1) ^ 2
            sum2Sq = sum2Sq + (values2(i) - mean2) ^ 2
        Next i
        correlation = sum / Sqr(sum1Sq * sum2Sq)
        RoundCorrelation = Round(correlation, decimals)
    End Function
End Module</code></pre>
<h3 id="example-3-grade-calculator">Example 3: Grade Calculator</h3>
<pre><code class="language-vbnet">&#x27; Calculate and round student grades
Class GradeCalculator
    Private m_roundingMode As String  &#x27; &quot;banker&quot;, &quot;up&quot;, &quot;down&quot;, &quot;nearest&quot;
    Public Sub Initialize(Optional roundingMode As String = &quot;banker&quot;)
        m_roundingMode = roundingMode
    End Sub
    Public Function CalculateFinalGrade(scores() As Double, _
                                       weights() As Double) As Double
        &#x27; Calculate weighted average grade
        Dim weightedSum As Double
        Dim totalWeight As Double
        Dim i As Integer
        weightedSum = 0
        totalWeight = 0
        For i = LBound(scores) To UBound(scores)
            weightedSum = weightedSum + scores(i) * weights(i)
            totalWeight = totalWeight + weights(i)
        Next i
        CalculateFinalGrade = RoundGrade(weightedSum / totalWeight)
    End Function
    Private Function RoundGrade(grade As Double) As Double
        &#x27; Round grade based on configured mode
        Select Case m_roundingMode
            Case &quot;banker&quot;
                RoundGrade = Round(grade, 1)
            Case &quot;up&quot;
                If grade &gt; Int(grade) Then
                    RoundGrade = Int(grade) + 1
                Else
                    RoundGrade = Int(grade)
                End If
            Case &quot;down&quot;
                RoundGrade = Int(grade)
            Case &quot;nearest&quot;
                If grade - Int(grade) &gt;= 0.5 Then
                    RoundGrade = Int(grade) + 1
                Else
                    RoundGrade = Int(grade)
                End If
            Case Else
                RoundGrade = Round(grade, 1)
        End Select
    End Function
    Public Function GetLetterGrade(numericGrade As Double) As String
        &#x27; Convert numeric grade to letter grade
        Dim rounded As Double
        rounded = RoundGrade(numericGrade)
        If rounded &gt;= 90 Then
            GetLetterGrade = &quot;A&quot;
        ElseIf rounded &gt;= 80 Then
            GetLetterGrade = &quot;B&quot;
        ElseIf rounded &gt;= 70 Then
            GetLetterGrade = &quot;C&quot;
        ElseIf rounded &gt;= 60 Then
            GetLetterGrade = &quot;D&quot;
        Else
            GetLetterGrade = &quot;F&quot;
        End If
    End Function
    Public Sub SetRoundingMode(mode As String)
        m_roundingMode = mode
    End Sub
    Public Function GetRoundingMode() As String
        GetRoundingMode = m_roundingMode
    End Function
End Class</code></pre>
<h3 id="example-4-measurement-precision-manager">Example 4: Measurement Precision Manager</h3>
<pre><code class="language-vbnet">&#x27; Manage precision for different types of measurements
Class MeasurementPrecisionManager
    Private Type MeasurementType
        Name As String
        Decimals As Integer
        Unit As String
    End Type
    Private m_types() As MeasurementType
    Private m_count As Integer
    Public Sub Initialize()
        m_count = 0
        ReDim m_types(0 To 99)
        &#x27; Add default measurement types
        AddMeasurementType &quot;Temperature&quot;, 1, &quot;°C&quot;
        AddMeasurementType &quot;Distance&quot;, 2, &quot;m&quot;
        AddMeasurementType &quot;Weight&quot;, 3, &quot;kg&quot;
        AddMeasurementType &quot;Pressure&quot;, 1, &quot;kPa&quot;
        AddMeasurementType &quot;Voltage&quot;, 2, &quot;V&quot;
    End Sub
    Public Sub AddMeasurementType(name As String, decimals As Integer, _
                                  unit As String)
        If m_count &gt; UBound(m_types) Then
            ReDim Preserve m_types(0 To UBound(m_types) + 50)
        End If
        m_types(m_count).Name = name
        m_types(m_count).Decimals = decimals
        m_types(m_count).Unit = unit
        m_count = m_count + 1
    End Sub
    Public Function RoundMeasurement(value As Double, _
                                    measurementType As String) As Double
        &#x27; Round value based on measurement type
        Dim i As Integer
        For i = 0 To m_count - 1
            If UCase(m_types(i).Name) = UCase(measurementType) Then
                RoundMeasurement = Round(value, m_types(i).Decimals)
                Exit Function
            End If
        Next i
        &#x27; Default to 2 decimals if type not found
        RoundMeasurement = Round(value, 2)
    End Function
    Public Function FormatMeasurement(value As Double, _
                                     measurementType As String) As String
        &#x27; Round and format measurement with unit
        Dim rounded As Double
        Dim i As Integer
        Dim decimals As Integer
        Dim unit As String
        decimals = 2
        unit = &quot;&quot;
        For i = 0 To m_count - 1
            If UCase(m_types(i).Name) = UCase(measurementType) Then
                decimals = m_types(i).Decimals
                unit = m_types(i).Unit
                Exit For
            End If
        Next i
        rounded = Round(value, decimals)
        FormatMeasurement = Format(rounded, &quot;0.&quot; &amp; String(decimals, &quot;0&quot;)) &amp; &quot; &quot; &amp; unit
    End Function
    Public Function GetPrecision(measurementType As String) As Integer
        Dim i As Integer
        For i = 0 To m_count - 1
            If UCase(m_types(i).Name) = UCase(measurementType) Then
                GetPrecision = m_types(i).Decimals
                Exit Function
            End If
        Next i
        GetPrecision = 2  &#x27; Default
    End Function
End Class</code></pre>
<h2 id="error-handling">Error Handling</h2>
<p>The <code>Round</code> function generates errors in specific situations:
<strong>Error 5: Invalid procedure call or argument</strong>
- Occurs when expression cannot be converted to numeric type
- Occurs when numdecimalplaces is excessively large
<strong>Error 6: Overflow</strong>
- Occurs when the rounded value exceeds the range of the return type
Example error handling:</p>
<pre><code class="language-vbnet">On Error Resume Next
Dim result As Double
result = Round(userInput, decimalPlaces)
If Err.Number &lt;&gt; 0 Then
    MsgBox &quot;Error rounding value: &quot; &amp; Err.Description
    result = 0
End If
On Error GoTo 0</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>Round</code> is very fast for reasonable decimal place values</li>
<li>Performance degrades with very large numdecimalplaces values</li>
<li>Consider caching rounded values if used repeatedly with same parameters</li>
<li>For large arrays, consider rounding in batches</li>
<li>Banker's rounding is slightly slower than simple truncation</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Understand Banker's Rounding</strong>: Be aware of round-to-even behavior for .5 values</li>
<li><strong>Document Rounding</strong>: Clearly document which rounding method is used</li>
<li><strong>Consistent Precision</strong>: Use same decimal places throughout calculations</li>
<li><strong>Round at Display Time</strong>: Keep full precision during calculations, round for display</li>
<li><strong>Avoid Cumulative Errors</strong>: Be aware of rounding errors accumulating in loops</li>
<li><strong>Test Edge Cases</strong>: Test with .5 values to verify rounding behavior</li>
<li><strong>Financial Calculations</strong>: Use 2 decimals for currency, consider legal requirements</li>
<li><strong>Validate Input</strong>: Check that decimal places parameter is reasonable</li>
<li><strong>Use Helper Functions</strong>: Wrap Round in domain-specific functions</li>
<li><strong>Consider Alternatives</strong>: For traditional rounding, implement custom function</li>
</ol>
<h2 id="comparison-with-related-functions">Comparison with Related Functions</h2>
<table>
<thead>
<tr>
<th>Function</th>
<th>Purpose</th>
<th>Behavior</th>
<th>Use Case</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Round</strong></td>
<td>Round to decimals</td>
<td>Banker's rounding</td>
<td>General rounding, financial</td>
</tr>
<tr>
<td><strong>Int</strong></td>
<td>Integer part</td>
<td>Truncate towards 0</td>
<td>Get whole number part</td>
</tr>
<tr>
<td><strong>Fix</strong></td>
<td>Integer part</td>
<td>Truncate towards 0</td>
<td>Get whole number part</td>
</tr>
<tr>
<td><strong><code>CInt</code></strong></td>
<td>Convert to integer</td>
<td>Round to nearest</td>
<td>Type conversion</td>
</tr>
<tr>
<td><strong><code>CLng</code></strong></td>
<td>Convert to long</td>
<td>Round to nearest</td>
<td>Type conversion</td>
</tr>
<tr>
<td><strong>Format</strong></td>
<td>Format number</td>
<td>Can specify decimals</td>
<td>Display formatting</td>
</tr>
</tbody>
</table>
<h2 id="platform-and-version-notes">Platform and Version Notes</h2>
<ul>
<li>Available in VB6 and VBA (added in VB6/Office 2000)</li>
<li>Uses banker's rounding (IEEE 754 standard)</li>
<li>Not available in earlier VB versions (use Int or Fix instead)</li>
<li>In VB.NET, replaced by Math.Round with <code>MidpointRounding</code> enum</li>
<li>Behavior differs from Excel's ROUND function (which uses "round half up")</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Banker's rounding may be unexpected for users familiar with traditional rounding</li>
<li>Cannot choose rounding mode (always banker's rounding)</li>
<li>Limited precision for very large decimal place values</li>
<li>Rounding errors can accumulate in iterative calculations</li>
<li>Different from Excel ROUND function behavior</li>
<li>No built-in round-up or round-down functions</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Int</code>: Returns the integer portion of a number (truncates toward zero)</li>
<li><code>Fix</code>: Returns the integer portion of a number (truncates toward zero)</li>
<li><code>CInt</code>: Converts expression to Integer with rounding</li>
<li><code>CLng</code>: Converts expression to Long with rounding</li>
<li><code>Format</code>: Formats number with specified decimal places</li>
</ul>
        </article>
        
        <div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
            <p>
                <a href="index.html">← Back to Math</a> |
                <a href="../index.html">View all functions</a>
            </p>
        </div>

    </main>

    <footer>
        <div class="container">
            <p>&copy; 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
        </div>
    </footer>
</body>
</html>