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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
//! # Round Function
//!
//! Returns a number rounded to a specified number of decimal places.
//!
//! ## Syntax
//!
//! ```vb
//! Round(expression, [numdecimalplaces])
//! ```
//!
//! ## Parameters
//!
//! - `expression` - Required. Numeric expression being rounded.
//! - `numdecimalplaces` - Optional. Number indicating how many places to the right of the decimal are included in the rounding. If omitted, integers are returned.
//!
//! ## Return Value
//!
//! Returns a value of the same type as `expression` that has been rounded to the specified number of decimal places.
//!
//! ## Remarks
//!
//! The `Round` 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.
//!
//! **Important Notes**:
//! - Uses banker's rounding (round to nearest even number when exactly halfway)
//! - If `numdecimalplaces` is omitted, returns an integer
//! - If `numdecimalplaces` is 0, rounds to nearest integer
//! - If `numdecimalplaces` is positive, rounds to that many decimal places
//! - If `numdecimalplaces` is negative, rounds to the left of decimal point
//! - Returns same data type as input expression
//!
//! **Banker's Rounding Examples**:
//! - 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)
//!
//! **Rounding Behavior by numdecimalplaces**:
//!
//! | numdecimalplaces | Effect | Example |
//! |------------------|--------|---------|
//! | Omitted | Round to integer | Round(2.7) = 3 |
//! | 0 | Round to integer | Round(2.7, 0) = 3 |
//! | Positive (e.g., 2) | Round to N decimals | Round(2.748, 2) = 2.75 |
//! | Negative (e.g., -1) | Round to left of decimal | Round(2748, -1) = 2750 |
//!
//! ## Typical Uses
//!
//! 1. **Financial Calculations**: Round currency values to 2 decimal places
//! 2. **Display Formatting**: Round numbers for user display
//! 3. **Statistical Analysis**: Round computed values to significant digits
//! 4. **Data Normalization**: Standardize precision across datasets
//! 5. **Measurement Values**: Round sensor readings to appropriate precision
//! 6. **Grade Calculation**: Round student scores to whole numbers
//! 7. **Percentage Display**: Round percentages to desired precision
//! 8. **Scientific Notation**: Round to significant figures
//!
//! ## Basic Examples
//!
//! ### Example 1: Round to Integer
//! ```vb
//! Dim value As Double
//! Dim rounded As Integer
//!
//! value = 3.7
//! rounded = Round(value)  ' Returns 4
//! ```
//!
//! ### Example 2: Round Currency
//! ```vb
//! Dim price As Double
//! Dim roundedPrice As Double
//!
//! price = 12.3456
//! roundedPrice = Round(price, 2)  ' Returns 12.35
//! ```
//!
//! ### Example 3: Banker's Rounding
//! ```vb
//! ' Demonstrates round-to-even behavior
//! Dim result1 As Integer
//! Dim result2 As Integer
//!
//! result1 = Round(2.5)  ' Returns 2 (rounds to even)
//! result2 = Round(3.5)  ' Returns 4 (rounds to even)
//! ```
//!
//! ### Example 4: Round to Tens
//! ```vb
//! Dim value As Long
//! Dim roundedToTens As Long
//!
//! value = 2748
//! roundedToTens = Round(value, -1)  ' Returns 2750
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: `RoundCurrency`
//! ```vb
//! Function RoundCurrency(amount As Double) As Double
//!     ' Round to 2 decimal places for currency
//!     RoundCurrency = Round(amount, 2)
//! End Function
//! ```
//!
//! ### Pattern 2: `RoundPercentage`
//! ```vb
//! Function RoundPercentage(percentage As Double, _
//!                         Optional decimals As Integer = 1) As Double
//!     ' Round percentage to specified decimal places
//!     RoundPercentage = Round(percentage, decimals)
//! End Function
//! ```
//!
//! ### Pattern 3: `RoundToSignificantFigures`
//! ```vb
//! Function RoundToSignificantFigures(value As Double, _
//!                                   sigFigs As Integer) As Double
//!     ' 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
//! ```
//!
//! ### Pattern 4: `RoundUpAlways`
//! ```vb
//! Function RoundUpAlways(value As Double, decimals As Integer) As Double
//!     ' Always round up (ceiling behavior)
//!     Dim factor As Double
//!     
//!     factor = 10 ^ decimals
//!     
//!     If value > 0 Then
//!         RoundUpAlways = Int(value * factor + 0.9999999) / factor
//!     Else
//!         RoundUpAlways = Int(value * factor) / factor
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 5: `RoundDownAlways`
//! ```vb
//! Function RoundDownAlways(value As Double, decimals As Integer) As Double
//!     ' Always round down (floor behavior)
//!     Dim factor As Double
//!     
//!     factor = 10 ^ decimals
//!     
//!     If value > 0 Then
//!         RoundDownAlways = Int(value * factor) / factor
//!     Else
//!         RoundDownAlways = Int(value * factor - 0.9999999) / factor
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 6: `RoundToNearest`
//! ```vb
//! Function RoundToNearest(value As Double, nearest As Double) As Double
//!     ' Round to nearest multiple of a number
//!     ' e.g., RoundToNearest(47, 5) = 45
//!     RoundToNearest = Round(value / nearest) * nearest
//! End Function
//! ```
//!
//! ### Pattern 7: `RoundArray`
//! ```vb
//! Sub RoundArray(arr() As Double, decimals As Integer)
//!     ' 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
//! ```
//!
//! ### Pattern 8: `RoundIfNeeded`
//! ```vb
//! Function RoundIfNeeded(value As Double, decimals As Integer, _
//!                       threshold As Double) As Double
//!     ' Only round if difference from rounded value exceeds threshold
//!     Dim rounded As Double
//!     
//!     rounded = Round(value, decimals)
//!     
//!     If Abs(value - rounded) > threshold Then
//!         RoundIfNeeded = rounded
//!     Else
//!         RoundIfNeeded = value
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 9: `RoundForDisplay`
//! ```vb
//! Function RoundForDisplay(value As Double) As String
//!     ' Round and format for display based on magnitude
//!     If Abs(value) < 0.01 Then
//!         RoundForDisplay = Format(Round(value, 4), "0.0000")
//!     ElseIf Abs(value) < 1 Then
//!         RoundForDisplay = Format(Round(value, 3), "0.000")
//!     ElseIf Abs(value) < 100 Then
//!         RoundForDisplay = Format(Round(value, 2), "0.00")
//!     Else
//!         RoundForDisplay = Format(Round(value, 0), "0")
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 10: `SymmetricRound`
//! ```vb
//! Function SymmetricRound(value As Double, decimals As Integer) As Double
//!     ' Traditional "round half up" instead of banker's rounding
//!     Dim factor As Double
//!     Dim shifted As Double
//!     
//!     factor = 10 ^ decimals
//!     shifted = value * factor
//!     
//!     If shifted >= 0 Then
//!         SymmetricRound = Int(shifted + 0.5) / factor
//!     Else
//!         SymmetricRound = Int(shifted - 0.5) / factor
//!     End If
//! End Function
//! ```
//!
//! ## Advanced Usage
//!
//! ### Example 1: Financial Calculator
//! ```vb
//! ' 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
//!         ' 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
//!         ' 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
//!         ' 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
//!         ' 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()
//!         ' 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
//!         
//!         ' 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
//! ```
//!
//! ### Example 2: Statistical Rounder
//! ```vb
//! ' Round statistical values to appropriate precision
//! Module StatisticalRounder
//!     Public Function RoundMean(values() As Double, decimals As Integer) As Double
//!         ' 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
//!         ' 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
//!         ' 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
//!         
//!         ' 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 > 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
//!         ' 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
//! ```
//!
//! ### Example 3: Grade Calculator
//! ```vb
//! ' Calculate and round student grades
//! Class GradeCalculator
//!     Private m_roundingMode As String  ' "banker", "up", "down", "nearest"
//!     
//!     Public Sub Initialize(Optional roundingMode As String = "banker")
//!         m_roundingMode = roundingMode
//!     End Sub
//!     
//!     Public Function CalculateFinalGrade(scores() As Double, _
//!                                        weights() As Double) As Double
//!         ' 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
//!         ' Round grade based on configured mode
//!         Select Case m_roundingMode
//!             Case "banker"
//!                 RoundGrade = Round(grade, 1)
//!                 
//!             Case "up"
//!                 If grade > Int(grade) Then
//!                     RoundGrade = Int(grade) + 1
//!                 Else
//!                     RoundGrade = Int(grade)
//!                 End If
//!                 
//!             Case "down"
//!                 RoundGrade = Int(grade)
//!                 
//!             Case "nearest"
//!                 If grade - Int(grade) >= 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
//!         ' Convert numeric grade to letter grade
//!         Dim rounded As Double
//!         
//!         rounded = RoundGrade(numericGrade)
//!         
//!         If rounded >= 90 Then
//!             GetLetterGrade = "A"
//!         ElseIf rounded >= 80 Then
//!             GetLetterGrade = "B"
//!         ElseIf rounded >= 70 Then
//!             GetLetterGrade = "C"
//!         ElseIf rounded >= 60 Then
//!             GetLetterGrade = "D"
//!         Else
//!             GetLetterGrade = "F"
//!         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
//! ```
//!
//! ### Example 4: Measurement Precision Manager
//! ```vb
//! ' 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)
//!         
//!         ' Add default measurement types
//!         AddMeasurementType "Temperature", 1, "°C"
//!         AddMeasurementType "Distance", 2, "m"
//!         AddMeasurementType "Weight", 3, "kg"
//!         AddMeasurementType "Pressure", 1, "kPa"
//!         AddMeasurementType "Voltage", 2, "V"
//!     End Sub
//!     
//!     Public Sub AddMeasurementType(name As String, decimals As Integer, _
//!                                   unit As String)
//!         If m_count > 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
//!         ' 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
//!         
//!         ' 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
//!         ' 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 = ""
//!         
//!         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, "0." & String(decimals, "0")) & " " & 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  ' Default
//!     End Function
//! End Class
//! ```
//!
//! ## Error Handling
//!
//! The `Round` function generates errors in specific situations:
//!
//! **Error 5: Invalid procedure call or argument**
//! - Occurs when expression cannot be converted to numeric type
//! - Occurs when numdecimalplaces is excessively large
//!
//! **Error 6: Overflow**
//! - Occurs when the rounded value exceeds the range of the return type
//!
//! Example error handling:
//!
//! ```vb
//! On Error Resume Next
//! Dim result As Double
//! result = Round(userInput, decimalPlaces)
//! If Err.Number <> 0 Then
//!     MsgBox "Error rounding value: " & Err.Description
//!     result = 0
//! End If
//! On Error GoTo 0
//! ```
//!
//! ## Performance Considerations
//!
//! - `Round` is very fast for reasonable decimal place values
//! - Performance degrades with very large numdecimalplaces values
//! - Consider caching rounded values if used repeatedly with same parameters
//! - For large arrays, consider rounding in batches
//! - Banker's rounding is slightly slower than simple truncation
//!
//! ## Best Practices
//!
//! 1. **Understand Banker's Rounding**: Be aware of round-to-even behavior for .5 values
//! 2. **Document Rounding**: Clearly document which rounding method is used
//! 3. **Consistent Precision**: Use same decimal places throughout calculations
//! 4. **Round at Display Time**: Keep full precision during calculations, round for display
//! 5. **Avoid Cumulative Errors**: Be aware of rounding errors accumulating in loops
//! 6. **Test Edge Cases**: Test with .5 values to verify rounding behavior
//! 7. **Financial Calculations**: Use 2 decimals for currency, consider legal requirements
//! 8. **Validate Input**: Check that decimal places parameter is reasonable
//! 9. **Use Helper Functions**: Wrap Round in domain-specific functions
//! 10. **Consider Alternatives**: For traditional rounding, implement custom function
//!
//! ## Comparison with Related Functions
//!
//! | Function | Purpose | Behavior | Use Case |
//! |----------|---------|----------|----------|
//! | **Round** | Round to decimals | Banker's rounding | General rounding, financial |
//! | **Int** | Integer part | Truncate towards 0 | Get whole number part |
//! | **Fix** | Integer part | Truncate towards 0 | Get whole number part |
//! | **`CInt`** | Convert to integer | Round to nearest | Type conversion |
//! | **`CLng`** | Convert to long | Round to nearest | Type conversion |
//! | **Format** | Format number | Can specify decimals | Display formatting |
//!
//! ## Platform and Version Notes
//!
//! - Available in VB6 and VBA (added in VB6/Office 2000)
//! - Uses banker's rounding (IEEE 754 standard)
//! - Not available in earlier VB versions (use Int or Fix instead)
//! - In VB.NET, replaced by Math.Round with `MidpointRounding` enum
//! - Behavior differs from Excel's ROUND function (which uses "round half up")
//!
//! ## Limitations
//!
//! - Banker's rounding may be unexpected for users familiar with traditional rounding
//! - Cannot choose rounding mode (always banker's rounding)
//! - Limited precision for very large decimal place values
//! - Rounding errors can accumulate in iterative calculations
//! - Different from Excel ROUND function behavior
//! - No built-in round-up or round-down functions
//!
//! ## Related Functions
//!
//! - `Int`: Returns the integer portion of a number (truncates toward zero)
//! - `Fix`: Returns the integer portion of a number (truncates toward zero)
//! - `CInt`: Converts expression to Integer with rounding
//! - `CLng`: Converts expression to Long with rounding
//! - `Format`: Formats number with specified decimal places

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn round_basic() {
        let source = r"
Dim result As Double
result = Round(3.7)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_with_decimals() {
        let source = r"
Dim rounded As Double
rounded = Round(12.3456, 2)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_if_statement() {
        let source = r#"
If Round(price, 2) > 100 Then
    MsgBox "Expensive"
End If
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_function_return() {
        let source = r"
Function RoundCurrency(amount As Double) As Double
    RoundCurrency = Round(amount, 2)
End Function
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_variable_assignment() {
        let source = r"
Dim value As Double
value = Round(inputValue, decimalPlaces)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_msgbox() {
        let source = r#"
MsgBox "Rounded: " & Round(pi, 3)
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_debug_print() {
        let source = r"
Debug.Print Round(value, 4)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_select_case() {
        let source = r#"
Select Case Round(score)
    Case 90 To 100
        grade = "A"
    Case 80 To 89
        grade = "B"
End Select
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_class_usage() {
        let source = r"
Private m_roundedValue As Double

Public Sub SetValue(value As Double)
    m_roundedValue = Round(value, 2)
End Sub
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_with_statement() {
        let source = r"
With calculation
    .Result = Round(.RawValue, .Precision)
End With
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_elseif() {
        let source = r#"
If Round(temp) < 0 Then
    status = "Freezing"
ElseIf Round(temp) > 30 Then
    status = "Hot"
End If
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_for_loop() {
        let source = r"
For i = 1 To 10
    rounded(i) = Round(values(i), 2)
Next i
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_do_while() {
        let source = r"
Do While Round(balance, 2) > 0
    balance = balance - payment
Loop
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_do_until() {
        let source = r"
Do Until Round(distance) >= target
    distance = distance + step
Loop
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_while_wend() {
        let source = r"
While Round(counter, 1) < 100.5
    counter = counter + increment
Wend
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_parentheses() {
        let source = r"
Dim val As Double
val = (Round(input, 3))
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_iif() {
        let source = r#"
Dim display As String
display = IIf(Round(value) > 10, "High", "Low")
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_nested() {
        let source = r"
Dim result As Double
result = Round(Round(value, 3) * 100, 1)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_array_assignment() {
        let source = r"
Dim prices(10) As Double
prices(i) = Round(rawPrices(i), 2)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_property_assignment() {
        let source = r"
Set obj = New Calculator
obj.RoundedValue = Round(obj.RawValue, 4)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_function_argument() {
        let source = r"
Call ProcessValue(Round(measurement, 2))
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_concatenation() {
        let source = r#"
Dim msg As String
msg = "Price: $" & Round(price, 2)
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_comparison() {
        let source = r#"
If Round(amount1, 2) = Round(amount2, 2) Then
    MsgBox "Equal"
End If
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_negative_decimals() {
        let source = r"
Dim roundedToTens As Long
roundedToTens = Round(2748, -1)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_bankers_rounding() {
        let source = r"
Dim r1 As Integer, r2 As Integer
r1 = Round(2.5)
r2 = Round(3.5)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_error_handling() {
        let source = r"
On Error Resume Next
Dim result As Double
result = Round(userInput, places)
If Err.Number <> 0 Then
    result = 0
End If
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn round_on_error_goto() {
        let source = r#"
Sub RoundValue()
    On Error GoTo ErrorHandler
    Dim r As Double
    r = Round(value, decimals)
    Exit Sub
ErrorHandler:
    MsgBox "Error rounding value"
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/round");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}