Re: Evaluate keyword rounding down rather that to the nearest number

3,193 views
Skip to first unread message

Tatu Aalto

unread,
Sep 28, 2013, 11:09:13 AM9/28/13
to will...@gmail.com, robotframe...@googlegroups.com
Ugh

I can not think an easy and clear way to do this, I think most useful
would be to write small keyword for it with python math [1] library. If
you satisfy to just round it down or up, that is quite easy:

round
${down} Evaluate math.floor((float(2)*float(100))/float(3)) math
${up} Evaluate math.ceil((float(2)*float(100))/float(3)) math

-Tatu
[1] http://docs.python.org/2/library/math.html

On 26.9.2013 17:07, Will Sehy wrote:
> "| | ${Variable}= | Evaluate | (2*100)/3" is returning 66 rather than
> 67. Is there a way to tell it to round up if the remainder is greater
> than or equal to .5?
> --
> You received this message because you are subscribed to the Google
> Groups "robotframework-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to robotframework-u...@googlegroups.com.
> To post to this group, send email to
> robotframe...@googlegroups.com.
> Visit this group at http://groups.google.com/group/robotframework-users.
> For more options, visit https://groups.google.com/groups/opt_out.

Tatu Aalto

unread,
Sep 28, 2013, 11:41:54 AM9/28/13
to will...@gmail.com, robotframe...@googlegroups.com
Ugh

And here is small python code that one can use as keyword:
import math

def _is_number(s):
    """
    Return True if s is a number or NaN
    Return False if s is not a number
    """
    try:
        float(s) # for int, long and float
    except ValueError:
        try:
            complex(s) # for complex
        except ValueError:
            return False

    return True

def number_rounding(number):
    """
    Rounds number down when it's decimal is smaller than .5
    Rounds number up when it's decimal is equal or bigger than .5
    Returns int when number can rounded. If it is not a number, returns a False
    """
    is_a_number = _is_number(number)
    if is_a_number:
        number = float(number) # Make number a float
       
        if math.isnan(number): # Check if the number is a NaN
            return False

        if number - math.floor(number) >= 0.5: # Round up
            return math.ceil(number)
        else:
            return math.floor(number) # Round down
    else:
        return False


-Tatu

Mikko Korpela

unread,
Oct 2, 2013, 3:08:45 AM10/2/13
to Tatu Aalto, will...@gmail.com, robotframework-users
Hello,

Anything inside evaluate is just plain python code. So you should be familiar with how python behaves with numbers (and what type numbers are when written in different ways).

Here:
*** Test Cases ***
Test
   ${a}=   Evaluate   (2*100)/3
   ${a}=   Evaluate   (2*100.0)/3
   ${a}=   Evaluate   round((2*100.0)/3)
   ${a}=   Evaluate   int(round((2*100.0)/3))



2013/9/28 Tatu Aalto <aalto...@gmail.com>



--
Mikko Korpela
Reply all
Reply to author
Forward
0 new messages