rounding in Python

LegacyForumLegacyForum Posts: 1,669 ✭✭
edited December 2016 in SOAtest
rounding a returned value in Python
Hi SoapTest,

I have the nice little script written that will return a value that I will be using to compare to one of my results. I am having a problem, however, with length of the value. I need it to be only two digits after the decimal point. I can't seem to find a way to truncate in Python.

Any suggetstions?

Here is my script:


from soaptest.api import *
from webtool.soap.parlay import *
from com.parasoft.api import *
from com.parasoft.util import *
from webtool.test import *
from time import *
from java.lang import *
from string import *


def genTimeStamp(input, context):
context.addDataSource("InternalDataSource1")
p = context.getValue("InternalDataSource1","BalAmtAll")

y=strftime("%Y", localtime())
j=strftime("%j", localtime())

y=atoi(y)
j=atoi(j)
p=atof(p)


if y == 2004:
m=(((366 - j) / 366.0) * p)
else:
m=(((365 - j) / 365.0) * p)

return SOAPUtil.getXMLFromString([str(m)])

def addDataSources(context):
return "Generated Data Sources"

What I get back is:
m= 33.60655737704918

I would like m to equal 33.61 in this case and take into account rounding.

Any suggestions?

Thanks a bunch,
Beth
Tagged:

Comments

  • LegacyForumLegacyForum Posts: 1,669 ✭✭
    edited August 2004
    Beth,

    Hi. Try using the round method. Here is a small example of how this works:
    CODE
    from soaptest.api import *

    def round_func(input, context):
     value = 32.456789
     valueRounded = round(value, 2)
     return valueRounded

    The above code would return "32.46" which is the original value rounded to two decimal places.

    Josh
  • LegacyForumLegacyForum Posts: 1,669 ✭✭
    Josh,

    Worked perfectly!!!!

    Thanks a bunch for the help,
    Beth
Sign In or Register to comment.