Forum Discussion

mrtibs's avatar
mrtibs
New Contributor
14 years ago

how to compare decimal values

Hi,

I need to compare a xsd:decimal (response) element with a value that I read from an Excel spreadsheet. Because the value that I need to read from the Excel spreadsheet needs to be assigned to a property, I need to convert it to a string. Unfortunately, I need to compare a string to a string. So, that means that I need to convert the returned xsd:decimal to a string as well... How do I do that? Am I going about this the wrong way? What is the best way of comparing number returned values?

Thanks,
T

2 Replies

  • vijayevol's avatar
    vijayevol
    Occasional Contributor
    //define two decimal variables (float) as
    def float a=0.0;
    def float b=0.0;

    //value "x" that you read from response message
    a=x.toFloat();;

    //value "y" that you read from excel

    b=y.toFloat();;

    //now compare
    if( a == b )
    {

    }
  • I had the same problem. I solved it by using the fact that you can use script code in property expansion, like this:

    Expected result:
    ${=new java.text.DecimalFormat("#.0000", new java.text.DecimalFormatSymbols(Locale.US)).format(${DataSource Excel#Sales price})}


    In this example my Excel has a property "Sales price", which is 12.30
    The database stores prices with 4 decimals, so the output of the XPath expression is : 12.3000

    The DecimalFormatSymbols(Locale.US) is to ensure the output is with a decimal . as our default locale uses a ,

    This way I do not have to create a script for every assertion.