I have a string like "3.9" I want to convert this string in to a number without using split function.
If string is 3.9 => o/p 39
If string is 1.2.3 => o/p 1开发者_运维百科23
I'm not sure what the purpose is. Would it work for your case to just remove the periods and parse the number?
int result = Int32.Parse(str.Replace(".", String.Empty));
You could remove replace the .
with empty string before trying to parse it:
string inputString = "1.2.3";
int number = int.Parse(inputString.Replace(".", ""));
string str = "3.9";
str = str.Replace(".","");
int i;
int.TryParse(str, out i);
I would probably go with something like this:
string str = "3.2";
str = str.Replace(".", "");
double number = convert.ToDouble(str);
you can use Replace(".",""); for this purpose
eg:
string stnumber= "5.9.2.5";
int number = Convert.ToInt32(stnumber.Replace(".", ""));
i think Convert.ToInt32();
is better than int.Parse();
精彩评论