Problem:
Using the sub routing below, when adding a value to another value the results eventually change from 2-decimal places to multiple decimal places.
Basically, the amount stored should always only be 2 decimal places, because the values passed in are always 2 decimal places.
Output from calling the sub routine multiple times.
...
Running total = 329430.75
New Withheld Amount = 710.79
Running total = 330141.54
New Withheld Amount = 616.54
Running total = 330758.07999999 9 <--Why does 330141.51 + 616.54 = this???
New Withheld Amount = 47.15
...
Code:
'Used to add running totals to a dictionary object
Notes:
As a workaround, I have a new routine that uses a custom round function to properly store only 2 decimal places - as the VB round function does not perform the type of rounding desired.
I understand that we are removing the value from the dictionary and adding it back...that's just the way the code was when I got here.
This function has been used for years and this is the first time I've seen anything like this. I have a working solution, so I'm not in any rush for an answer but was just curious if anyone has seen this before or might have a reason for its occurrence.
Using the sub routing below, when adding a value to another value the results eventually change from 2-decimal places to multiple decimal places.
Basically, the amount stored should always only be 2 decimal places, because the values passed in are always 2 decimal places.
Output from calling the sub routine multiple times.
...
Running total = 329430.75
New Withheld Amount = 710.79
Running total = 330141.54
New Withheld Amount = 616.54
Running total = 330758.07999999 9 <--Why does 330141.51 + 616.54 = this???
New Withheld Amount = 47.15
...
Code:
'Used to add running totals to a dictionary object
Code:
Sub AddRunningTotal(ByRef dicObject, strName, dblValue) If IsNull(dicObject) OR dicObject is Nothing then Exit Sub if dicObject.Exists( strName) then dblTemp = dicObject.Item( strName) dblTemp = dblTemp + dblValue dicObject.Remove strName dicObject.Add strName, dblTemp else dicObject.Add strName, dblValue end if End Sub
As a workaround, I have a new routine that uses a custom round function to properly store only 2 decimal places - as the VB round function does not perform the type of rounding desired.
I understand that we are removing the value from the dictionary and adding it back...that's just the way the code was when I got here.
This function has been used for years and this is the first time I've seen anything like this. I have a working solution, so I'm not in any rush for an answer but was just curious if anyone has seen this before or might have a reason for its occurrence.
Comment