Forum Discussion

rickh_28's avatar
rickh_28
Occasional Contributor
11 years ago

Financial Troubles?

My system under test has a finance piece, and I'm running into trouble with the way TestComplete handles non-integers.  As far as I can tell, my numerical options are to use integers or floats.  The problem is, integers can't easily be used to handle decimal points and floats are inherently inaccurate.  So occasionally, when I try to store 351148.87, it instead gets stored as 351148.866666667. 

 

This causes two problems.  First, the initial checkpoints (verifying the expense was entered correctly) fail.  Then later on, when I'm checking to make sure the expeses are all added correctly, I get a second failure because all of those small rounding errors start to add up.  In my dataset of 200 expenses (totaling just over $1 million), I have a discrepancy of something like 36 cents. 

 

So, my question is, does anyone else out there have a similar problem?  How do you deal with it?

7 Replies

  • Are you scripting this?

     

    What language?

     

    As I suspect the language in use is likely to affect the options available ...

    • rickh_28's avatar
      rickh_28
      Occasional Contributor

      I'm using JScript.  I know it doesn't support decimal types, and I've thought about switching languages, but I'm not as familiar with the other available options  I looked at C# Script a bit, but it seems to have the same limitations as JScript (unless I'm missing something).  Which one would you suggest?

       

      Also, I'm still hoping to salvage what I have for this project - I have about 2 weeks worth of development sunk into it using JScript, and I would hate to have to scrap it all and go back to the drawing board.  Are there any possible workarounds?

    • rickh_28's avatar
      rickh_28
      Occasional Contributor

      Let me clarify my previous response a bit.  As far as I can tell, there is no way to explicitly cast a variable as any specific type in any of the lanugages supported by TestComplete, except DelphiScript and VBScript.  In the case of DelphiScript, the only base types it supports are integer, float, boolean and string.

       

      Which leaves me with VBScript.  According to Microsoft, VBScript does support a currency data type (but still no decimal), so that would work as far as that goes. But in researching this, I couldn't help but notice that 'currency' only goes to 4 decimal places. Does that mean that I could, theoretically, get a similar (if not equal) level of precision by just rounding my floats to 4 decimal places?  That would certainly be a lot easier than trying to refactor my scripts. And if I can get a sense that this is generally accepted as a "good enough" practice, I might just do that.

       

      Thoughts?

      • HKosova's avatar
        HKosova
        Icon for Alumni rankAlumni

        How about using the .NET Decimal type? It has 28-29 significant digits, and is more presice than the scripting data types.

         

        // JScript
        var value = dotNET.System.Decimal.zctor_5(351148.87); // from float
        var value2 = dotNET.System.Decimal.Parse("351148.87"); // from string

        Note that you'll probably need to handle Decimals using their own methods - .CompareTo, .Equals, .Add, .Subtract and so on.