By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,131 Members | 1,749 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

VBScript Scripting Dictionary Decimal Problem

P: 8
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.079999999 <--Why does 330141.51 + 616.54 = this???
New Withheld Amount = 47.15
...


Code:
'Used to add running totals to a dictionary object
Expand|Select|Wrap|Line Numbers
  1. Sub AddRunningTotal(ByRef dicObject, strName, dblValue)
  2.     If IsNull(dicObject) OR dicObject is Nothing then Exit Sub
  3.  
  4.     if dicObject.Exists( strName) then
  5.         dblTemp = dicObject.Item( strName)
  6.         dblTemp = dblTemp + dblValue
  7.         dicObject.Remove strName
  8.         dicObject.Add strName, dblTemp
  9.     else
  10.         dicObject.Add strName, dblValue
  11.     end if
  12. End Sub
  13.  
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.
Mar 13 '09 #1
Share this Question
Share on Google+
2 Replies


tlhintoq
Expert 2.5K+
P: 3,525
Basically, the amount stored should always only be 2 decimal places, because the values passed in are always 2 decimal places.
100.44
+ 100.56
----------
= 201 <--- Not two decimal places even though both additives were

Moral of this post: Don't make assumptions

Since we can't see the dicObject class which is where dicObject.item is coming from its hard to say what's happening in the unseen realm.
Mar 13 '09 #2

P: 8
Thanks for the response.

Sorry, I should have posted "Basically, the amount stored should always only be 2 decimal places (or less), because the values passed in are always 2 decimal places." If I were passing in a value with 4 decimal places, I would expect that value to be added properly as well.

Unfortunately, one of the benefits of code reuse is also a pitfall - when the code is expected to perform "properly" and does not.

(I'm asking for it by programming in vbscript anyway and much prefer programming in a strongly-typed language like C# or Java when I can.)
Mar 13 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.