I have the following code and when I use it in certain circumstances
the rounding is weird. I have been through this for hours and can't
see any obvious issue and would appreciate any suggestions....
Example I have 5 items @ $12.55 each = $62.75
Customer pays 25% = $15.69
Insurance covers 75% = $47.06.......but when I wrap the following
function around the numbers, the 75% portion comes out as $47.006 and
causes a 5 cent error....
Any help or pointers would be appreciated.
Thanks
John
Public Function MakeMoney(ByVal dblNumber As Double) As Double
Dim strMoney As String
Dim intLength As Integer
Dim strDollar As String
Dim intDollar As Integer
Dim strCents As String
Dim strExtraDecimals
Dim intCents As Integer
Dim intDecimal As Integer
Dim strSign As String
'Turn the money value into a string.
strMoney = CStr(dblNumber)
intLength = Len(strMoney)
If Left(strMoney, 1) = "-" Then
intLength = intLength - 1
strMoney = Right(strMoney, intLength)
strSign = "-"
Else
strSign = ""
End If
'Find out where the decimal is in the string.
intDecimal = InStr(1, strMoney, ".")
If intDecimal = 0 Then
'There were no decimals to work with.
MakeMoney = dblNumber
Exit Function
End If
'Get the Dollars to the left of the decimal and turn it into a
string.
intDollar = intDecimal - 1
strDollar = Left(strMoney, intDollar)
'Get the Cents to the right of the decimal and turn it into a
string.
intCents = intLength - intDecimal
strCents = Right(strMoney, intCents)
If intCents > 2 Then
strExtraDecimals = Right(strCents, intCents - 2)
strCents = Left(strCents, 2)
strExtraDecimals = Left(strExtraDecimals, 1)
If CInt(strExtraDecimals) > 4 Then
If CInt(strCents) < 99 Then
strCents = CStr(CInt(strCents) + 1)
Else
strDollar = CStr(CInt(strDollar) + 1)
strCents = "00"
End If
Else
'Leave the dollars and cents as they were
End If
Else
'There were under 3 deciamal places so you didn't need to do
anything.
MakeMoney = dblNumber
Exit Function
End If
If CInt(strCents) < 10 Then
strCents = "0" + strCents
End If
'Put the Dollars and 2 digit Cents back together.
strMoney = strSign + strDollar + "." + strCents
'Change the String Back into a Double.
MakeMoney = CDbl(strMoney)
End Function
dblvar = 123.45
intvar = cint(dblvar)
? intvar
123
cents = dblvar-intvar
? cents
0.450000000000003
? Cint(cents * 100)
45
? format(-123.45,"#,###.00")
-123.45
? format(-123.456,"#,###.00")
-123.46
Here's a little sub for testing some things out
Sub Vars()
Dim s As String
Dim c As Currency
Dim sign As String
s = Format(-123.45, "#,###.00")
msgbox s
c = Format(-123.45, "#,###.00")
msgbox c
sign = IIf(c < 0, "-", "")
msgbox sign
msgbox CCur(123.4567)
msgbox CCur(Format(123.4567, "#,###.00"))
End Sub
Hi Flymo,
Currency values are reals (single or double), and therefore they are
not exact. Also, in calculating percentages the result is a real.
With currency values you can then get fractions of cents. In further
calculations you can have considerable rounding differences or
unwanted accumulation of fractions of cents.
With Format or DecimalPlaces you can hide these fractions, but they
still are there!
To prevent unwanted situations you have to get rid of these cent
fractions. For that purpose you can use the next function:
Function In_cents(cur_value)
In_cents = Int(100 * cur_value + 0.5) / 100
End Function
HBInc.
Thanks for the suggestions, I wil give a try
John