|
|
Title | Use the VBA library to round to a specific number of decimal places |
Description | This example shows how to use the VBA library to round to a specific number of decimal places in Visual Basic 6. |
Keywords | VBA, round, decimal places |
Categories | Algorithms, Miscellany |
|
|
When you change the TextBox's value, the program displays the value rounded to 0, 1, and 2 decimal places.
|
|
Private Sub txtValue_Change()
Dim value As Single
Dim i As Integer
value = CSng(txtValue.Text)
For i = 0 To 2
lblRounded(i).Caption = VBA.Round(value, i)
Next i
End Sub
|
|
VBA.Round uses "banker's rounding." That means if the digits to be dropped contain exactly 5, then VBA.Round rounds the value to the nearest even number. For example:
VBA.Round(1.235, 2) = 1.24
VBA.Round(1.245, 2) = 1.24
VBA.Round(1.255, 2) = 1.26
etc.
For a lot more discussion on rounding, see Round a value to a specified number of digits.
|
|
|
|
|
|