Consider the average of four numbers {4.1, 4.1, 4.1, 4.3}, Dutch rounded to one decimal place.
The average of the four numbers is exactly 4.15; when using VBA's Round
function it correctly returns 4.2. However, if I give it the expression that calculates the average it rounds incorrectly: Round((4.1 + 4.1 + 4.1 + 4.3)/4, 1)
returns 4.1. I can coax a correct
answer by doing something like Round(Round((4.1 ...)/4), 5), 1)
so I suspect it has something to do with how results of expressions are stored. I also tried multiplying each number by 10 then dividing by 40, but the rounding was still incorrect.
? (4.1 + 4.1 + 4.1 + 4.3)/4
4.15
? Round(4.15, 1)
4.2
? Round((4.1 + 4.1 + 4.1 + 4.3)/4, 1)
4.1
? Round(Round((4.1 + 4.1 + 4.1 + 4.3)/4, 5), 1)
4.2
? Round((4.1*10 + 4.1*10 + 4.1*10 + 4.3*10)/40, 1)
4.1
Question: In Excel/VBA, what's the difference between being given a float directly and being given an expression that evaluates as a float?
This is because you are adding floating point numbers combined with the buggy
Round
.You can trick those values to become true decimals by forcing that on only one factor or addend, and
Round
will round to even, for example:You can also use
Format
, which does true 4/5 rounding:To overcome these quirks completely, you can use my rounding functions, here
RoundMid
which also does true 4/5 rounding:Full code at VBA.Round.