473,320 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Maths Problem

Hi All,

I am having problems adding and subtracting in ASP, i am using a MySQL database and connecting via MyODBC using a preset DSN connection.

So i have an online store, in the table i am trying to add two fields (total_products) and (total_shipping) and then subtract (total_discounts).

All the above fields are Decimal (10,2) type in the database. If i run a simple query <%=FormatNumber(rs("total_products"))%> the values are fine.

However when i try to actually add and subtract them i get an error, i think the error is because the values are strings.

Code is below thank you;

Expand|Select|Wrap|Line Numbers
  1. TotalPrice = rs("total_products")
  2. TotalPrice = TotalPrice + rs("total_shipping")
  3. TotalPrice = TotalPrice - rs("total_discounts")
  4.  
This code creates the below error;

Expand|Select|Wrap|Line Numbers
  1. Microsoft VBScript runtime error '800a000d' 
  2. Type mismatch 
  3.  
  4. /assets/includes/admin/orders_orders.asp, line 87 
  5.  
Line 87 is line 2 in the code above, the values in the database are;

total_products = 1844.51
total_shipping = 18.97
total_discounts = 221.34

as mentioned above these three fields are decimal(10,2).

All help appreciated

Regards,
Justin
Jul 27 '08 #1
2 1454
DrBunchman
979 Expert 512MB
Hi Justin,

Try using the CDbl() function to cast your variables to a data type of double. This will force the script to treat them as decimals rather than strings. e.g.
Expand|Select|Wrap|Line Numbers
  1. TotalPrice = Cdbl(rs("total_products"))
  2. TotalPrice = Cdbl(TotalPrice) + Cdbl(rs("total_shipping"))
  3. TotalPrice = Cdbl(TotalPrice) - Cdbl(rs("total_discounts"))
  4.  
Alternatively you could perform the calculation within your SQL like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT (total_products + total_shipping - total_discounts) AS TotalPrice
  3.  
Let me know if this helps,

Dr B
Jul 28 '08 #2
Solution worked absoloutly perfectly, thank you!

Hi Justin,

Try using the CDbl() function to cast your variables to a data type of double. This will force the script to treat them as decimals rather than strings. e.g.
Expand|Select|Wrap|Line Numbers
  1. TotalPrice = Cdbl(rs("total_products"))
  2. TotalPrice = Cdbl(TotalPrice) + Cdbl(rs("total_shipping"))
  3. TotalPrice = Cdbl(TotalPrice) - Cdbl(rs("total_discounts"))
  4.  
Alternatively you could perform the calculation within your SQL like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT (total_products + total_shipping - total_discounts) AS TotalPrice
  3.  
Let me know if this helps,

Dr B
Jul 28 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jack Higgs | last post by:
Has anyone got any ideas how I could go about producing a maths program for Primary School pupils? The coding isn't such a problem (ie random numbers etc), it's acctually figuring out how to...
5
by: Scott Gunn | last post by:
Hello All, In VB.Net 2003 why is this the result to a simple maths calculation? 3.53 - 3.52 = 0.00999999046325684 Its not just this one either try this: 1.001 - 0.001 = 0.999999999999989
20
by: Daisy | last post by:
Something simple, I'm sure. My code: System.Windows.Forms.MessageBox.Show(((Int32)Math.Ceiling(1966 / 100)).ToString()); I'm expecting 20, but getting 19. What am I doing wrong? Thanks --
9
by: uttre | last post by:
hai to all, i did some programming in Lisp (6 months) & next i want to learn C++. i searched all the archives of "comp.lang.c++" & ACCU too & decided "C++ Primer" 3/e as my text book....
5
by: Ibys | last post by:
Hi, i am just starting to learn javascript, so i am probably doing something very simple wrong. i have read a lot of articles on maths in java, but cant find anything simple enough for my problem. I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.