469,282 Members | 2,120 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Addition in ASP

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
Jul 19 '05 #1
3 5002
shvar=shvar+cint(rstemp4("servhours"))

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

"Jay" <jl****@optonline.net> wrote in message
news:FT***********************@news4.srv.hcvlny.cv .net...
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

Jul 19 '05 #2

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


On Sat, 10 Jan 2004 21:11:01 GMT, "Jay" <jl****@optonline.net> wrote:
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


Jul 19 '05 #3
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/


"Brynn" <z@z.com> wrote in message
news:40**************@news.comcast.giganews.com...

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


On Sat, 10 Jan 2004 21:11:01 GMT, "Jay" <jl****@optonline.net> wrote:
I have two strings that instead of adding them together to get the sum theare 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

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mark Dickinson | last post: by
2 posts views Thread by akickdoe22 | last post: by
24 posts views Thread by Alex Vinokur | last post: by
5 posts views Thread by Paul | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.