vb5prgrmr
Posting Virtuoso
1,912 posts since Mar 2009
Reputation Points: 156
Solved Threads: 296
abu taher
Practically a Posting Shark
845 posts since Jul 2008
Reputation Points: 14
Solved Threads: 78
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.
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
Only a pleasure dixie. Happy coding....
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350