Sum not matching?

Author Message
aryx

  • Total Posts : 10
  • Scores: 0
  • Reward points : 0
  • Joined: 10/20/2005
  • Status: offline
Sum not matching? Monday, January 30, 2012 3:08 AM (permalink)
0
I'm trying to add some numbers together and it should end up beeing zero. But instead I get -9,094...
It seems to be the decimals that is the cause, why is this happening and how can I solve it?
 
strTotSum=-12698.2
strTotCash=5016
strTotWeb=2265
strTotCheque=0
strTotCard=5417.2
msgbox strTotSum + strTotCash + strTotWeb + strTotCheque + strTotCard 
 
 
#1
    ebgreen

    • Total Posts : 8227
    • Scores: 98
    • Reward points : 0
    • Joined: 7/12/2005
    • Status: offline
    Re:Sum not matching? Monday, January 30, 2012 3:15 AM (permalink)
    0
    Actually the result that you are getting is .000000000000909494701772928
     
    This has to do with the way that vbs handles non-integers. If for instance you are doing financial calculations (so the decimal will at most be hundredths) then I would do it like this:
     
    strTotSum=-12698.2 * 100
    strTotCash=5016 * 100
    strTotWeb=2265 * 100
    strTotCheque=0 * 100
    strTotCard=5417.2 * 100
    wscript.echo (strTotSum + strTotCash + strTotWeb + strTotCheque + strTotCard)/100

    "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
    Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
    http://www.visualbasicscript.com/m_47117/tm.htm
     
    #2
      aryx

      • Total Posts : 10
      • Scores: 0
      • Reward points : 0
      • Joined: 10/20/2005
      • Status: offline
      Re:Sum not matching? Monday, January 30, 2012 4:03 AM (permalink)
      0
      Great, thanks for the solution!
       
      #3

        Online Bookmarks Sharing: Share/Bookmark

        Jump to:

        Current active users

        There are 0 members and 1 guests.

        Icon Legend and Permission

        • New Messages
        • No New Messages
        • Hot Topic w/ New Messages
        • Hot Topic w/o New Messages
        • Locked w/ New Messages
        • Locked w/o New Messages
        • Read Message
        • Post New Thread
        • Reply to message
        • Post New Poll
        • Submit Vote
        • Post reward post
        • Delete my own posts
        • Delete my own threads
        • Rate post

        2000-2012 ASPPlayground.NET Forum Version 3.9