Formatting issue with trailing zeros
Fellow SOATesters:
I have a problem with formatting price extracted to a Databank stored in a script variable and then using it after some arithmetic - for example (price multiplied by quantity to arrive at total price for the cart items, add tax and finally arrive at total amount ).
Sample prices are 90.00, 102.41, 50.88
Qty can 1, 2 etc.
Tax can be 0.00 or 0.12 etc., depending on the situation
This is what I'm doing using an extension tool and Jython script:
from com.parasoft.api import *
from java.util import *
from soaptest.api import *
def calcTotals(input, context):
to get list price
try: price1 = context.getValue("Generated Data Source", "Test 2: ListPrice1") Application.showMessage("The price1 is: " + price1)
...
...
to get quantity
try: qty1 = context.getValue("CustomerAndProduct","Quantity1") Application.showMessage("qty1 is: " + qty1)
to obtain the final price of the cart items
totalPriceOfItemsInCart = ( (float(price1)int(qty1)) + (float(price2)int(qty2)) )
Application.showMessage("The totalPriceOfItemsInCart is: " + str(totalPriceOfItemsInCart) )
to get total cost
TotalCost = ( totalPriceOfItemsInCart + float(FreightTotal) + float(TaxLine) + float(TaxFrt) ) Application.showMessage("The TotalCost is: " + str(TotalCost) )
Here is the snippet of the Console logs:
....
Scenario: GetProductPriceOfCartItems
Test 1: GetProductPricing(ProductPricingRequest) - success
set Test 2: ListPrice1=90.00
set Test 2: ListPrice2=null
Scenario: FreightWithAddress
Test 1: FreightCalc - success
get custType=New
set Test 1: FreightCharge=0.00
Scenario: TaxCalcWithAddress
Test 1: TaxCalculation - success
get custType=New
set Test 1: TotalLineTax=0.00
The price1 is: 90.00
qty1 is: 1
The totalPriceOfItemsInCart is: 90.0
The FreightTotal is: 0.00
The TaxLine is: 0.00
The TotalCost is: 90.0
Now the problem:
The variable "totalPriceOfItemsInCart" and "TotalCost" are missing a trailing zero (for example 90.0 instead of 90.00). How do I arrive at proper formatting? I don't have an issue with prices not having a trailing zero or the Total Amount (Total Cost + Tax) not ending with a trailing zero.
I need the script to run on my servers (I'm using SOATest 9.10 CLI on Linux platform) so I want the logic to be as simple as possible and with a vanilla SOATest installation as much as possible. I don't want every one my testing team to have to install or configure something for this script to run as intended on their individual SOATest installations.
Thank you and appreciate your help.
Comments
-
Use Decimal instead of float. See https://docs.python.org/3/library/decimal.html. For example:
from com.parasoft.api import * from decimal import * def calculate(): price = "1.00" quantity = "3" totalPrice = Decimal(price)*int(quantity) Application.showMessage("totalPrice: " + str(totalPrice))
0 -
I get this error with Decimal import:
Traceback (most recent call last):, File "", line 4, in , ImportError: No module named decimal,
How can this be fixed?
Thanks for your help.
0 -
Ahh, there was a step I missed. You need to download and install Jython 2.5.3 (https://www.jython.org/downloads.html), and then configure your preferences by going to Parasoft > Scripting and setting the "Jython home" field to the location where you installed Jython. After configuring the preferences, restart SOAtest and it should work.
0