by Ertan Zanagar (2 Submissions)
Category: Math/Dates
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Mon 8th July 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Rounding numbers in VB is tricky. Round() function does not round $$ amounts correctly. This function rounds a number (2 decimals) without
Dim intNumber
Dim intDecimalLocation
Dim intDecimalNumber, intOriginalDecimalNumber
Dim intNextToNumber
Dim intOriginalNumber
If IsNumeric(piInNumber) = False Then Exit Function
intOriginalNumber = piInNumber
intNumber = Int(piInNumber)
intDecimalLocation = InStr(1, piInNumber, ".")
piInNumber = Mid(piInNumber, 1, intDecimalLocation + 2)
intDecimalNumber = Mid(piInNumber, intDecimalLocation + 1, 2)
intNextToNumber = Mid(intOriginalNumber, intDecimalLocation + 2 + 1, 1)
If IsNumeric(intNextToNumber) Then
If intNextToNumber >= 5 And IsNull(intNextToNumber) = False Then
intOriginalDecimalNumber = intDecimalNumber
intDecimalNumber = intDecimalNumber + 1
If LenB(intDecimalNumber) < LenB(intOriginalDecimalNumber) Then
intDecimalNumber = "0" & intDecimalNumber
End If
If LenB(intDecimalNumber) > LenB(intOriginalDecimalNumber) Then
intNumber = intNumber + 1
RoundDollars = intNumber
Else
intNumber = intNumber & "." & intDecimalNumber
RoundDollars = intNumber
End If
Else
RoundDollars = intNumber & "." & intDecimalNumber
End If
Else
RoundDollars = intOriginalNumber
End If
End Function
No comments have been posted about Rounding numbers in VB is tricky. Round() function does not round $$ amounts correctly. This functi. Why not be the first to post a comment about Rounding numbers in VB is tricky. Round() function does not round $$ amounts correctly. This functi.