2 decimal points in robot framework

4,983 views
Skip to first unread message

Ridwan Abdullah

unread,
Dec 7, 2016, 5:12:51 AM12/7/16
to robotframework-users
Good Day

I have a problem converting a number to 2 decimal points.
For example:
${number1}=    set variable    ${1.3333}
${number1}=    convert to number         ${number1}     2

The result for ${number1} is 1.33 <-- correct

However if I put something like this:
${number1}=    set variable    ${0.0}
${number1}=    convert to number         ${number1}     2

I expect to get 0.00 but the result remains 0.0

Any response would be appreciated.
Thank you

Laurent Bristiel

unread,
Dec 7, 2016, 6:11:03 PM12/7/16
to robotframework-users
Hi,

this is the way Python works: it does not show trailing zeros.
You can try this in Python:
>>> 123.123000
123.123
>>> 0.0000
0.0

So, if you want to have trailing zeros anyway, you have to switch to strings and do something like:
>>> "%.2f" % round(1.3333,2)
'1.33'
>>> "%.2f" % round(0.0,2)
'0.00'

Source: http://stackoverflow.com/questions/19986662/rounding-a-number-in-python-but-keeping-ending-zeros

Hope this helps,
Laurent
Reply all
Reply to author
Forward
0 new messages