Addition in ASP

J

Jay

I have two strings that instead of adding them together to get the sum the
are concatenating together. Does anyone know how I can get these two to
add.

while not rstemp4.eof
vservdate = rstemp4("servdate")
vdesc = rstemp4("desc")
vservhours2 = rstemp4("servhours")
vtravelhours = rstemp4("travelhours")
vtech = rstemp4("tech")
shvar = shvar + rstemp4("servhours")
thvar = thvar + rstemp4("travelhours")

rstemp4.movenext
wend
 
T

Tom B

shvar=shvar+cint(rstemp4("servhours"))

assuming, of course that servhours only contains numbers (no strings or
nulls)
 
B

Brynn

I thought it was kinda buggy the other day in my code ... I had

<%
'// string becaused passed from the datasource as such
firstNum = "1"
secondNum = "1.5"

firstNum = FormatNumber(firstNum, 1)
secondNum = FormatNumber(secondNum, 1)

total = firstNum + secondNum
%>

total was equalling 11.5 ... the strings combined

so I changed to this, and it worked

total = 0 + firstNum + secondNum

I don't know if it is because it understood to use addition because I
had the number 0 there ... that's my only guess ... I would like to
hear more on this anyone

Brynn
 
A

Aaron Bertrand [MVP]

All types are variants in VBScript. There is a concept called data type
precedence which matches types together (sort of like a lowest common
denominator) if the engine has no idea what you *meant* the types to be.

Add to this that we have a multi-purpose operator (many people use addition
+ for string concatenation as well).

So, to make sure numbers are treated as numbers, cast them explicitly using
CInt, CLng, CDbl. Formatnumber keeps them as a string, so this doesn't help
any.

--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top