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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string to double

Status
Not open for further replies.

rebShaul

Programmer
Jun 22, 2003
15
IL
Hello.
I want to get a number (double) from the user and store it in a double variable.
I thought the simplest way to get an input is using textBox. The problem is, that the data format of the text at the textBox is String and I want the double value of the data.

Thanks for your help,
Shaul.
 
Well, you can use a text box and extract the left side and the right side of the string, but I think the best way is to use NumericUpDown. You can set the decimal places, increment, max value, min value and other properties. This control return its value as decimal and not as string. It also ensures that the user won't type letters into it.
To get the value:
Code:
double d;
d = (double)NumericUpDown1.Value;
 
you should verify textBox.text first though, else watch your program crash when daft things get entered. this is a way of getting round tha problem, though probably not my neatest code:

char[] sin = textbox1.Text.ToCharArray();
string sout = "0";
foreach(char c in sin)
{
if(((int)c>47 && (int)c<58) || c == '.')
{
sout += c.ToString();
}
else
{
break;
}
}
double myDouble = double.Parse(sout);
 
senbo, thank you vey much.
Your solution solves the problem when you're obliged to use textBox. Otherwise, korach's solution might be pretty good.

Any way,
thank you very much again,
Shaul.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top