Take the 2-minute tour ×

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 123

share|improve this question
And "o/p" stands for ... ? –  Henk Holterman Apr 13 '11 at 9:04
output, maybe ? –  SirDarius Apr 13 '11 at 9:06
add comment

5 Answers

up vote 5 down vote accepted

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));
share|improve this answer
It appears the op wants to be able to store floating points so wouldn't a double be more suitable? –  m.edmondson Apr 13 '11 at 8:58
@m.edmondson: In both the examples the output is an integer value. –  Guffa Apr 13 '11 at 9:29
Really, why is this? Do you have any links to point me to some information? –  m.edmondson Apr 13 '11 at 9:40
@m. edmondson: What are you talking about? Look at the examples in the question. –  Guffa Apr 13 '11 at 10:26
Sorry I see what you mean, I was under the impression he wanted to store 3.9 as a datatype not 39. My mistake. –  m.edmondson Apr 13 '11 at 11:15
add comment

You could remove replace the . with empty string before trying to parse it:

string inputString = "1.2.3";
int number = int.Parse(inputString.Replace(".", ""));
share|improve this answer
add comment
string str = "3.9";
str = str.Replace(".","");

int i;
int.TryParse(str, out i);
share|improve this answer
add comment

I would probably go with something like this:

string str = "3.2";
str = str.Replace(".", "");

double number = convert.ToDouble(str);
share|improve this answer
this will not work :( Use, str=str.Replace(".", "");. –  Javed Akram Apr 13 '11 at 8:58
Try it now - I was missing a bit out –  m.edmondson Apr 13 '11 at 8:59
@m.edmoundson Why double? He's after ints –  razlebe Apr 13 '11 at 10:36
@razlebe - Because 3.2 can't be represented as an int, in the question all he said was string in to a number. Double is a number. –  m.edmondson Apr 13 '11 at 11:13
@m.edmondson He clearly specifies that he's looking to strip out the decimal points too. –  razlebe Apr 13 '11 at 11:31
show 1 more comment

you can use Replace(".",""); for this purpose


string stnumber= "";
int number =  Convert.ToInt32(stnumber.Replace(".", ""));

i think Convert.ToInt32(); is better than int.Parse();

share|improve this answer
Whether Convert.ToInt32 or Int32.Parse is better depends on the expected behaviour. Convert.ToIn32 will for example convert an empty string into zero, which may not always be desirable. –  Guffa Apr 13 '11 at 9:32
add comment

Your Answer


