Here is some more explaination that doesn't require deep thought
Option Explicit
Sub SimpleTest()
Dim SomeNumberA As Double
Dim SomeNumberB As Currency
Dim SomeNumberC As Single
SomeNumberA = 8.950568
SomeNumberB = 8.950568
SomeNumberC = 8.950568
'Floating points are only usefull for scientific notation (Single and
Double)
Debug.Print "Single " & (Int(SomeNumberC * 100)) / 100 & " is a " &
TypeName((Int(SomeNumberC * 100)) / 100)
Debug.Print "Double " & (Int(SomeNumberA * 100)) / 100 & " is a " &
TypeName((Int(SomeNumberA * 100)) / 100)
Debug.Print "Rounded Double " & (Int(Round(SomeNumberA, 2) * 100)) / 100 & "
is a " & TypeName((Int(Round(SomeNumberA, 2) * 100)) / 100)
'Scaled values should be used when precision counts
' Visual basic does not have a decimal value type
' but it does have a CDec (Convert to Decimal) operator
Debug.Print "Decimal " & (Int(CDec(SomeNumberA) * 100)) / 100 & " is a " &
TypeName((Int(CDec(SomeNumberA) * 100)) / 100)
' Currency Value types are the next best thing and will maintain precision
' notice this becomes a double again after all the operations are done
Debug.Print "Currency " & (Int(SomeNumberB * 100)) / 100 & " is a " &
TypeName((Int(SomeNumberB * 100)) / 100)
' A quick work around to your delima
Debug.Print "Rounded Decimal " & Round(CDec(SomeNumberA), 2) & " is a " &
TypeName(Round(CDec(SomeNumberA), 2))
' Another way to skin the cat
Debug.Print "Special " & SpecialReduce(SomeNumberA, 2) & " is a " &
TypeName(SpecialReduce(SomeNumberA, 2))
End Sub
'
Public Function SpecialReduce(InputValue As Variant, Places As Integer) As
Currency
If Not IsNumeric(InputValue) Then
Err.Raise 13, "My Module", "InputValue must be of numeric type."
End If
SpecialReduce = Round(CCur(InputValue), Places)
End Function
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:u8**************@TK2MSFTNGP14.phx.gbl...
"Andrew Taylor" <an***********@cantab.net> schrieb: As always with this type of problem, the answer is that you can't rely
on floating point numbers to give exact results.
Additional information:
<URL:http://groups.google.de/group/microsoft.public.dotnet.languages.vb/msg/1d103c8590802792>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>