Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse the VBA library to round to a specific number of decimal places
DescriptionThis example shows how to use the VBA library to round to a specific number of decimal places in Visual Basic 6.
KeywordsVBA, round, decimal places
CategoriesAlgorithms, 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.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated