Connecting Tech Pros Worldwide Forums | Help | Site Map

Adding decimals from a database

Member
 
Join Date: Jul 2007
Posts: 70
#1: Aug 4 '09
Hi

I have a simple code which needs to add 2 decimals together but alas, it won't. The figures are stored on an SQL database (as a decimal). The site uses asp (vb script)
Lets pretend MaleHeight = 1.2 and FemaleHeight = 2.5
Expand|Select|Wrap|Line Numbers
  1.  <%
  2. dim h1, h2, total
  3. h1 = rsPCB.Fields.Item("MaleHeight").Value 
  4. h2 = rsPCB.Fields.Item("FemaleHeight").Value   
  5. total = h1 + h2
  6. Response.Write(total)
  7. %>
Line 5 (total = h1 + h2) gives me the error Type mismatch.

If I add cInt to h1 and h2, it does the maths but doesn't show the decimal place. The result is 3 instead of 3.7

However, if I add Response.Write("h1= "& h1 &"") before line 5, the result is 1.2! I can't see why total can't add/display the result.

Any ideas?

Member
 
Join Date: Jul 2007
Posts: 70
#2: Aug 4 '09

re: Adding decimals from a database


http://www.w3schools.com/vbScript/func_csng.asp
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#3: Aug 5 '09

re: Adding decimals from a database


I assume that since you found the csng function on w3schools, you got your thingy working. the reason cint didn't work is that it converts the argument into an integer (cint stands for Convert INTeger). Anyway, glad you got it working.

Jared
Member
 
Join Date: Jul 2007
Posts: 70
#4: Aug 6 '09

re: Adding decimals from a database


Hi Jhardman.

Thanks, yes all working. I find it strange in VBscript you don't need to declare the variable type which is why I couldn't work out what to do. Unless there is a way to do dim variable as string etc

Thanks

Dave
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#5: Aug 7 '09

re: Adding decimals from a database


Quote:

Originally Posted by DaveRook View Post

Hi Jhardman.

Thanks, yes all working. I find it strange in VBscript you don't need to declare the variable type which is why I couldn't work out what to do. Unless there is a way to do dim variable as string etc

Thanks

Dave

You are definitely right, that is one of the biggest drawbacks of VBScript. And since I was working with VBScript before I was a serious professional, it is something I still think about backwards sometimes. Anyway, glad you got it up.

Jared
Reply


Similar ASP / Active Server Pages bytes