Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Calculating

Ikasari Widiyanto
Greenhorn
Posts: 29
Hi,

For example, I have 3 columns, description of items, quantity and unit cost, in the database, For example:
Description Quantity unitcost
item1 10 2
item2 10 3
these are retrieved from the database. How can I automatically calculate the total cost of these 2 things? Anybody can help me?

Balan Raj
Ranch Hand
Posts: 74
If u have HTML + JavaScript , make javascript to do it

better use something like JSP to do it

Ikasari Widiyanto
Greenhorn
Posts: 29
Yah I am using javascript to count it since it is faster but how? any sample code?

this is the javascript for calculate:
function calculate(){
var unitPrice = document.test.unitcost.value
var quantity = document.test.quantity.value

if (isNaN(unitPrice)||unitPrice.length==0){
alert ("Enter the unit Price in numeric")
}else if (isNaN(quantity)||quantity.length==0){
alert ("Enter the quantity in numeric")
}else{
var amount = unitPrice * quantity
amount = round(amount,3)
document.test.totalcost.value=amount
}

}

this is the part where i retrieve from database
<table width="878" border="1">
<tr>
<td width="355">Description:</td>
<td width="58">Quantity:</td>
<td width="70">Unit Cost: </td>
<td width="367">Recommended Supplier:</td>
</tr>
<%
System.out.println("requestview1.jsp:"+requestNo);
conn = db.getConnection();
stmt = conn.createStatement();
sql = "select * from requestdetail where requestNo="+requestNo+" order by id asc";
rs = stmt.executeQuery(sql);
while (rs.next())
{
description = rs.getString("description");
unitcost = rs.getString("unitcost");
quantity = rs.getString("quantity");
supplier = rs.getString("supplier");
%>
<tr>
<td><%= description %></td>
<td><%= unitcost %> </td>
<td><%= quantity %></td>
<td><%= supplier %></td>
</tr>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</table>

Yuriy Fuksenko
Ranch Hand
Posts: 413
I am using javascript to count it since it is faster

I don't think it is faster Javascript is generally much slowlier than anything else you use (Unless you handling user input - than it wins if you don't have any fancy i18n validation)

By the way, did you think about calculating it right in the SQL?
Something like

I am not applying any functions to convert Strings to numbers, but I don't think it is hard to do.