Hi, i have a project to do in VB and its been a while i done anything in it.
My problem is taxation and decimal places.

How can I round 2.19 to 2.20 ?

the actuall number was 2.194 and vb6 rounds it at 2.19, is this as it should behave?

tnx

## All 7 Replies

Check out the Round function and the Format function and for how vb rounds, see some of the articles this search brought up...

Good Luck

this is enough what vb5prgrmr said.

You can either use the format function as in -

``````txtAnswer.Text = Format(p - (a * f - ct), "##,##0")
'Using the 0(zero) at the end will round the number off to a zero.``````

Or you can use the round function as in -

``````Dim TotCost As Single
Dim srTotal As String
Dim Quarters As Integer
'   This routine assumes that you have created a form
'    containg a text box for entering the amount to be rounded called "txtCost",
'    a command button called "cmdRound" to do the calculation,
'    and a label to display the result called "lblRounded".
'    Any amount half a penny above the 25 cent mark will be rounded up
'    to the next quarter dollar amount.

Code

Private Sub cmdRound_Click()

TotCost = Val(txtCost.Text)  'insert entered value into var
srTotal = CCur(TotCost) 'round value to two decimals and convert to string
srTotal = Format(srTotal, "currency") 'force string to give 2 decimal places

'round up to next highest quarter dollar
If Val(Right\$(srTotal, 2)) > 75 Then
srTotal = Int(srTotal) + 1
ElseIf Val(Right\$(srTotal, 2)) > 50 Then
Quarters = 3
srTotal = Int(srTotal) + (0.25 * Quarters)
ElseIf Val(Right\$(srTotal, 2)) > 25 Then
Quarters = 2
srTotal = Int(srTotal) + (0.25 * Quarters)
ElseIf Val(Right(srTotal, 2)) > 0 Then
Quarters = 1
srTotal = Int(srTotal) + (0.25 * Quarters)
End If

srTotal = Format(srTotal, "currency") 'convert string to currency again
lblRounded.Caption = srTotal            'display rounded value in label

End Sub``````

The exact amount is not always given, generating errors. This is something done a while back which works perfectly.

``````'Rounding numbers in VB is tricky. Round() function does not round \$\$ amounts correctly. This function rounds a number (2 decimals) without using the Round() function

Public Function RoundDollars(piInNumber)

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``````

I hope this helps towards you solving your problem.

tnx guys u have been very helpfull!!!

Only a pleasure dixie. Happy coding....