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?


Recommended Answers

All 7 Replies

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.


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
            intNumber = intNumber & "." & intDecimalNumber
            RoundDollars = intNumber
        End If
        RoundDollars = intNumber & "." & intDecimalNumber
    End If
    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....

How about using rnd function

dim total1 as double,hrs as single, rate as single

total1 = format(rnd(hrs*rate),"###,##0.00")

Random???? I guess u ment round? :
Anyways im OK now, everything is cool and working fine!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.