Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have a string variable and in this string i have a number like this "2.0.0.0"
and now i want to convert it to number variable, i tried but i couldn't Convert it,
how can i Convert it to number?
Posted
Comments
Tomas Takac 8-Nov-14 3:39am    
And what do you expect the resulting value would be?

you could consider that if you remove the '.'s, maybe using string.Replace(".","") then, each position represents thousands, hundreds, tens and units - then convert the string with the '.'s remove to an integer - no pun intended, but Im not sure what value that really gives you, storing it as a number
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 8-Nov-14 5:17am    
Exactly! A number would only hold one decimal place in double, float or decimal format. Removing 3 or (total number of periods - 1) would resolve this error. See Solution 1 for this.
Garth J Lancaster 8-Nov-14 5:33am    
yeah - sorry mate, seems we posted at the same time - I didn't see yours before I posted - yours said the same thing :-)
Afzaal Ahmad Zeeshan 8-Nov-14 5:34am    
No need for the sorry brother. Correct answer come up in everyone's mind with basic concepts. +5 for you.
No (existing) number format can accept it. Floating point numbers would also allow only one decimal point. Your number contains 4.

Remove the . from your string, and then convert it. Or atleast allow only 1 decimal point in your string.

If you're having one decimal point, you can still convert it to Double[^], Float[^] etc numbers. Even if you have one decimal point, you will have to convert it to decimal or float, conversion to an integer with a single decimal point would cause the error.

There is a better way of converting the strings to integers and getting to know whether they can be converted or not, using a TryParse[^] in your code, which would either return a true if number was converted or a false if there were any errors in the format.

Remember the size of the data type

Even if you convert the string to a numeric data type, the result would be based on the size of the data type. You need to consider the size of the data type you're going to use.

http://msdn.microsoft.com/en-us/library/ms228360(v=vs.90).aspx[^]
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900