File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes Calculating Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Calculating" Watch "Calculating" New topic


Ikasari Widiyanto

Joined: May 31, 2004
Posts: 29

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

Joined: Aug 26, 2003
Posts: 74
If u have HTML + JavaScript , make javascript to do it

better use something like JSP to do it
Ikasari Widiyanto

Joined: May 31, 2004
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")
var amount = unitPrice * quantity
amount = round(amount,3)


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

Joined: Feb 02, 2001
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.
I agree. Here's the link:
subject: Calculating
It's not a secret anymore!