Search code examples
robotframework

How to get 2 decimal places without rounding up the number in robot framework?


I am trying to only use 2 decimals places of a number without rounding off.

For example. 1.027 should give after removing the last digit as 1.02

${result}= Evaluate "%.2f" % 1.027

${result} = 1.03 #but I would like it to be 1.02


Solution

  • Below code is currently working for me:

    *** Settings ***
    Documentation
    
    *** Variables ***
    ${MYNUMBER1}        1.329999999
    ${MYNUMBER2}        1.027
    
    *** Test Cases ***
    Get Exact Two Decimal Number
        ${result1}         Evaluate  math.floor(${MYNUMBER1} * 100) / 100    modules=math
        Log To Console     My first two decimal exact number is ${result1}!
        ${result2}         Evaluate  math.floor(${MYNUMBER2} * 100) / 100    modules=math
        Log To Console     My second two decimal exact number is ${result2}!
    

    Output is:

    ==============================================================================
    Testdemo6
    ==============================================================================
    Get Two Decimal Number                                                
    ..My first two decimal exact number is 1.32!
    ..My second two decimal exact number is 1.02!
    Get Two Decimal Number                                                | PASS |
    ------------------------------------------------------------------------------
    Testdemo6                                                             | PASS |
    1 test, 1 passed, 0 failed
    ==============================================================================
    Output:  C:\Development\robot-scripts\Test_Test\results\output.xml
    Log:     C:\Development\robot-scripts\Test_Test\results\log.html
    Report:  C:\Development\robot-scripts\Test_Test\results\report.html
    

    Hopefully this helps.