VBScript Scripting Dictionary Decimal Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rosco
    New Member
    • Dec 2007
    • 8

    VBScript Scripting Dictionary Decimal Problem

    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
    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
    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.
    Last edited by tlhintoq; Mar 13 '09, 04:50 PM. Reason: [CODE] ...your code here... [/CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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.

    Comment

    • Rosco
      New Member
      • Dec 2007
      • 8

      #3
      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.)

      Comment

      Working...