Adding decimals from a database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaveRook
    New Member
    • Jul 2007
    • 147

    Adding decimals from a database

    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
    Code:
     <%
    dim h1, h2, total
    h1 = rsPCB.Fields.Item("MaleHeight").Value 
    h2 = rsPCB.Fields.Item("FemaleHeight").Value   
    total = h1 + h2
    Response.Write(total)
    %>
    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?
  • DaveRook
    New Member
    • Jul 2007
    • 147

    #2
    Fixed

    Sorry! We can't seem to find the resource you're looking for

    Comment

    • jhardman
      Recognized Expert Specialist
      • Jan 2007
      • 3405

      #3
      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

      Comment

      • DaveRook
        New Member
        • Jul 2007
        • 147

        #4
        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

        Comment

        • jhardman
          Recognized Expert Specialist
          • Jan 2007
          • 3405

          #5
          Originally posted by DaveRook
          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

          Comment

          Working...