Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

rounding in Python

LegacyForum
LegacyForum Posts: 1,664 ✭✭
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

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    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
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Josh,

    Worked perfectly!!!!

    Thanks a bunch for the help,
    Beth