Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Total figure not accurate?
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Total figure not accurate?

 Post Reply Post Reply
Author
ngaisteve1 View Drop Down
Groupie
Groupie
Avatar

Joined: 26 December 2002
Location: Malaysia
Status: Offline
Points: 169
Post Options Post Options   Thanks (0) Thanks(0)   Quote ngaisteve1 Quote  Post ReplyReply Direct Link To This Post Topic: Total figure not accurate?
    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.

Back to Top
Bullschmidt View Drop Down
Groupie
Groupie


Joined: 31 May 2003
Location: United States
Status: Offline
Points: 72
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bullschmidt Quote  Post ReplyReply Direct Link To This Post 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
Classic ASP Design Tips, ASP Web Database Sample (Freely Downloadable)
Back to Top
ljamal View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 16 April 2003
Status: Offline
Points: 888
Post Options Post Options   Thanks (0) Thanks(0)   Quote ljamal Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.