471,599 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 software developers and data experts.

converting textbox.text to real number

How do I convert a value in a textbox to a real number so I can perform some
math ops on it? I understand I will need to validate the textbox's input to
make sure it is a real number and not a string so any suggestions along
those lines are also appreciated.

Thanks,
Scott
Nov 17 '05 #1
3 11039
OK, I did some more searching and found I can use the Convert class
(convert.todouble) to convert the string to a double. It works but is this
the best way?

I'm still looking for how to validate the input so any help there is
appreciated.

Thanks,
Scott
"Homer Simpson" <homer@moe's.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
How do I convert a value in a textbox to a real number so I can perform
some math ops on it? I understand I will need to validate the textbox's
input to make sure it is a real number and not a string so any suggestions
along those lines are also appreciated.

Thanks,
Scott

Nov 17 '05 #2
double d = 0;
try{d=double.Parse(yourtextbox.Text)
} catch {}

"Homer Simpson" <homer@moe's.com> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
OK, I did some more searching and found I can use the Convert class
(convert.todouble) to convert the string to a double. It works but is this
the best way?

I'm still looking for how to validate the input so any help there is
appreciated.

Thanks,
Scott
"Homer Simpson" <homer@moe's.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
How do I convert a value in a textbox to a real number so I can perform
some math ops on it? I understand I will need to validate the textbox's
input to make sure it is a real number and not a string so any
suggestions along those lines are also appreciated.

Thanks,
Scott


Nov 17 '05 #3
Hi Scott,

Convert will thrown an exception if it fails. You can use

bool success = double.TryParse(...)

You can also limit the TextBox input to numbers only + any special characters like decimal point etc. Handle the TextBox.KeyPress event for this.
On Sun, 24 Jul 2005 06:46:13 +0200, Homer Simpson <homer@moe's.com> wrote:
OK, I did some more searching and found I can use the Convert class
(convert.todouble) to convert the string to a double. It works but is this
the best way?

I'm still looking for how to validate the input so any help there is
appreciated.

Thanks,
Scott
"Homer Simpson" <homer@moe's.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
How do I convert a value in a textbox to a real number so I can perform
some math ops on it? I understand I will need to validate the textbox's
input to make sure it is a real number and not a string so any suggestions
along those lines are also appreciated.

Thanks,
Scott



--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Brian Robbins | last post: by
1 post views Thread by Shawn | last post: by
12 posts views Thread by Frederik Vanderhaeghe | last post: by
16 posts views Thread by mj.redfox.mj | last post: by
25 posts views Thread by Blasting Cap | last post: by
2 posts views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.