Converting text to numbers?

E

Ed Richter

I'm trying to add some variables together as shown below, but not getting the results I'm looking for. The problem appears to be that it's taking the variables from the form and treating them as text rather than numbers, therefore not able to add them. Instead it's just concatenating the variables together. What do I need to do to convert the variables such as Q1_R1_value to a variable rather than text? Do I just need to declare them in a Dim statement or is more needed?

Q1_R1_value = Request.Form("Q1_R1")
Q2_R1_value = Request.Form("Q2_R1")
Q3_R1_value = Request.Form("Q3_R1")

R1_sum = Q1_R1_value
R1_sum = R1_sum + Q2_R1_value
R1_sum = R1_sum + Q3_R1_value
 
T

Trevor L.

Ed said:
I'm trying to add some variables together as shown below, but not
getting the results I'm looking for. The problem appears to be that
it's taking the variables from the form and treating them as text
rather than numbers, therefore not able to add them. Instead it's
just concatenating the variables together. What do I need to do to
convert the variables such as Q1_R1_value to a variable rather than
text? Do I just need to declare them in a Dim statement or is more
needed?

Q1_R1_value = Request.Form("Q1_R1")
Q2_R1_value = Request.Form("Q2_R1")
Q3_R1_value = Request.Form("Q3_R1")

R1_sum = Q1_R1_value
R1_sum = R1_sum + Q2_R1_value
R1_sum = R1_sum + Q3_R1_value

Try the unary operator +
e.g.
R1_sum = +Q1_R1_value
R1_sum = +R1_sum + Q2_R1_value
R1_sum = +R1_sum + Q3_R1_value

Or, a bit shorter
R1_sum = +Q1_R1_value + Q2_R1_value + Q3_R1_value

It should work
 
S

Stefan B Rusynko

If the form fields are truly numbers use
Q1_R1_value = Cint(Request.Form("Q1_R1"))
Q2_R1_value = Cint(Request.Form("Q2_R1"))
Q3_R1_value = Cint(Request.Form("Q3_R1"))

- will fail if your users can enter anything but a number in the form
(use FP validation to prevent that)

Or w/ ASP you can check for the values as numbers

If IsNum(Request.Form("Q1_R1")) Then Q1_R1_value = Cint(Request.Form("Q1_R1"))


--

_____________________________________________
SBR @ ENJOY (-: [ Microsoft MVP - FrontPage ]
"Warning - Using the F1 Key will not break anything!" (-;
To find the best Newsgroup for FrontPage support see:
http://www.frontpagemvps.com/FrontPageNewsGroups/tabid/53/Default.aspx
_____________________________________________


I'm trying to add some variables together as shown below, but not getting the results I'm looking for. The problem appears to be
that it's taking the variables from the form and treating them as text rather than numbers, therefore not able to add them. Instead
it's just concatenating the variables together. What do I need to do to convert the variables such as Q1_R1_value to a variable
rather than text? Do I just need to declare them in a Dim statement or is more needed?

Q1_R1_value = Request.Form("Q1_R1")
Q2_R1_value = Request.Form("Q2_R1")
Q3_R1_value = Request.Form("Q3_R1")

R1_sum = Q1_R1_value
R1_sum = R1_sum + Q2_R1_value
R1_sum = R1_sum + Q3_R1_value
 
E

Ed_R

OK that was the solution I was looking for. The users enter the
valsues via drop down fields, so nothing but numbers can be entered. I
knew I had to add some type of command or "method" in front of them to
convert to numbers, but couldn't recall the exact format.

Further down the code I then "Insert" them into a database. Currently
it's formatted as ' " & Q1_R1_value & " ', ' " & Q2_R1_value & " ',
etc.... I believe once I convert them to numbers, I'll then need to
change the formatting, I think I need to remove the single apostophe so
the format will then look like: " & Q1_R1_value & " , " & Q2_R1_value
& " , etc....
Is that correct?
 

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

Top