Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Try to Add 3 Numeric Values but Cannot??????? 3

Status
Not open for further replies.

IEAN

Programmer
Sep 13, 2003
122
US
I am stuck on a ridiculously simply problem. I am trying to add 3 numbers together var1+var2+var3. I MADE SURE that all 3 vars are of Numeric type, but when I add them together, it throws a "hey buddy, you are trying to add a 'string' together" type error. At best it just combines all 3 variables like var1var2var3 instead of adding them together. Why??? All 3 vars are Numeric, why are they not adding up together?
 
Without seeing code it's hard to say, but try some tests on your own to see if you can get down to the bottom of it.

Run the IsNumeric function to see if the variables are indeed numbers.
Code:
Response.Write IsNumeric(var1)
Response.Write IsNumeric(var2)
Response.Write IsNumeric(var3)
Try casting them as integers to see if you get the correct value.
Code:
Cint(var1)+Cint(var2)+Cint(var3)

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
Hi DNG, Kaht, thank you for your reply,

DNG: I tried cdbl(var1)+cdbl(var2)+cdbl(var3) and it worked! Thank you so much DNG!

Kaht: I tried to run the IsNumeric function as you said and it turned out the vars returned False as nummeric, how come? In the database the datatype is of "Number" type and I also "formatnumber" them too before I add them together. How could they not be numeric? Any ideas?
 
Like I said above, it's really hard to tell w/o seeing your code. However, when I pull data I cast each value just to ensure that I don't run into these problems, better safe than sorry.

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
I believe that the formatnumber function is designed to display a number as a string in a given format, rather than turning it into a number. Just remove the formatnumber function. If you want to convert a string representation of a number into an actual number use the CSNGL, CDBL or CINT function as needed

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Poor CLng(), completely forgotten in john's list above ;)
 
... and CByte and CCur (even though these are strictly variant subtypes you can still calculate with them)

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
True...and technically you can add stuf to cDate() :)
 
However the key point of my post was to point out OP's fundamental error regarding FormatNumber. Avoiding that would render all the CDbl (or whatever) redundant, as we wouldn't be creating the problem before we solve it!

It's a classic case of 'Ask the wrong question, get the wrong answer!' [smile]

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top