Print Page | Close Window

Total figure not accurate?

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=3565
Printed Date: 30 March 2026 at 3:03am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Total figure not accurate?
Posted By: ngaisteve1
Subject: Total figure not accurate?
Date Posted: 16 June 2003 at 12:44am

<%
Do While Not rs.EOF
strTotal = rs("qty_issued") * rs("stock_price")
response.write "<tr><td>" & rs("stock") & "</td>"
response.write "<td>" & rs("qty") & "</td>"
response.write "<td>" & formatnumber(strTotal,2) & "</td></tr>"
strGrandTotal = clng(strGrandTotal) + clng(strTotal)
rs.MoveNext
Loop
response.write "<tr><td><b>Grand Total = " & formatnumber(strGrandTotal,2) & "</b></td></tr>"
%>

I wonder why my grand total figure not accurate esp the total of cents. Sample
Stock Qty $
Flyers 600 162.36
chair 1 16.50
Grand total = 178.00

How do I fix this? Thank you.




Replies:
Posted By: Bullschmidt
Date Posted: 16 June 2003 at 1:00am

So that your cents add up correctly perhaps change this:

rs("qty_issued") * rs("stock_price")

To instead be this:

Round(rs("qty_issued") * rs("stock_price"), 2)

And change this:

strGrandTotal = clng(strGrandTotal) + clng(strTotal)

To be this instead:

strGrandTotal = cdbl(strGrandTotal) + cdbl(strTotal)

As a minor point you might want to change the names of variables strTotal and strGrandTotal to be dblTotal and dblGrandTotal instead so that people looking at the code don't think that the variables are storing strings.  Or you might use cur instead of dbl for a prefix to mean currency.



-------------
J. Paul Schmidt, Freelance ASP Web Developer
www.Bullschmidt.com - www.Bullschmidt.com
Classic ASP Design Tips, ASP Web Database Sample (Freely Downloadable)


Posted By: ljamal
Date Posted: 16 June 2003 at 2:51am
CLng is a long integer and does not store decimal places, thus you will lose you cents.
I would not round the prices, I would use the FormatCurrency function. Rounding will just produce more rounding errors.

The information is being called from a database so do the math there. Instead of doing
select qty_issued, stock_price from ...
use
select qty_issued, stock_price, qty_issued*stock_price as subtotal from ...

for the grand total use
select sum (qty_issued*stock_price) as total from ....

This will alleviate all the headache of scripting the arithmetic.

-------------
L. Jamal Walton

http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net