Yes, use the Format() function to round with when the total number of digits (ALL digits) to round to are not more than 15.
Ignore the following if the reasons (which I understand them to be) do not interest anyone.
It, the Format() function, will round up to a total of 15 digits (not decimal places, but TOTAL digits). After that 15 digits, the rest get turned into zeros. The format function uses a Decimal number type for it's accuracy.
It works similar to this:
[blue]
Code:
Public Function RoundX(ByVal dNumber As Double, Optional ByVal lDecimalPlaces As Integer = 2) As Double
'Can only round up to 15 digits, the integer portion having precedence.
'Works like the Format$() function.
Dim xFactor As Double
xFactor = CDec(10 ^ lDecimalPlaces)
RoundX = Fix(CDec(dNumber * xFactor + (Sgn(dNumber) * 0.5))) / xFactor
End Function
[black]
(I hope that's the right one)
The problem with using a double is that not all decimals numbers can be represented in binary.
Using a fixed decimal, we can correct this problem up to a certain limit.
The Round() function uses statistical rounding.
This means the following:
1. It is agreed that numbers less than 5 round down.
2. It is agreed that numbers greater than 5 round up.
3. But what about 5? It is exactly "midway". Rounding either way would be actually incorrect, and when working with statistics, rounding that 5 always up would lead to bad statistics.
We could do this: Round 5 up except every other occurance which we would round it up instead (the first 5 round up, the second round down, the third round up, etc.)
Well, we would need to create a function that would round a GROUP of values in order to keep track of the occurances of 5.
The Round() function works with just ONE value at a time. So what it does is just this:
If the digit before the 5 is an odd number, then round up, otherwise round down. As seen in
strongm's post, or the thread I mentioned.
You can see that this would also lead to an inaccuracy when used on a GROUP of values - especially if all values in that group which have a 5 as the last digit to round to, also have as the digit just before the 5, an even digit (or an odd digit, or more evens than odd, etc.)
It's also not correct...just closer to correct statistical rounding.