开发者

Convert string in to number?

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();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜