A word of warning:
The CDec and CCur must be handled with care, and their usage must be thouroghly thought out before implementing.
It is a scaled integer data type (fixed-point data type), just like the CCur() function.
Even though it will handle a relative large number for business purposes, for anything else it may get you into trouble.
-It will handle a total of 15 digits to the left of the decimal when no digits are to the right:
999,999,999,999,999.0
- It will handle a total of 15 digits including in the count all digits to the left and right of the decimal:
9,999,999.99999999
9,999,999,999.99999
So, 15 digits and the decimal point can be anywhere
- It will handle a total of 28 digits to the Right of the decimal when no digits are present on the left of the decimal:
0.1234567890123456789012345678
more digits may not produce an error but will be returned with an unexpected value, it will start rounding and chopping of:
CDec(99,999,999,999.99999) 16 digits results in 100,000,000,000.0 Rounded
But a lower whole number with too many digits will cause the decimal digits to get chopped off:
?CDec(1,234,567,890.123456) returns 1,234,567,890.123456
But ?CDec(12345678901.123456) returns 1,234,567,890.12345
The last 5 got chopped off.
Therefore, for normal financial operations, if you convert the values with CDec or CCur before calculating, you'll probably be alright.
As said, the same problem lies with the CCur; the advantage with the CCur is that you can define a variable as currency to prevent problems, and with Decimal, there isn't a variable type (except variant)
Otherwise use CSng, and for extremely high numbers use CDbl.
And it is best to use CSng or CDbl with the example above, or better yet, use the function above that uses the dSubTotal variable.
Then there shouldn't be any problems at all.
(I also changed the xFactor variable type in order to handle larger numbers)
Public Function RoundX(ByVal dNumber As Double, Optional ByVal lDecimalPlaces As Long = 2) As Double
Dim xFactor As Double
xFactor = 10 ^ lDecimalPlaces
RoundX = Fix(CDbl(dNumber * xFactor + (Sgn(dNumber) * 0.5))) / xFactor
End Sub
[/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!