I have an Asp.net text box that is being auto populated by javascript using Google Maps Api.
The text in the textbox can show any amount of miles from 40.2 – 1,634 mi ect.
I need to take the text from the text box and put it into an int to be stored in the DB.
So really I need a couple things to happen. The text always shows as “40.2 mi” (without the quotes) and what I need is just a round number. or Int. like “40” or “1634”.
So I can store this in my DB table. I’ve tried Substring and Math.Round. But I keep getting exceptions. My most recent attempt is this:
string miles = txtEstDistance.Text;
miles.Substring(0, miles.IndexOf('.') > 0 ? miles.IndexOf('.') : miles.Length);
int oDistanceMiles = Convert.ToInt32(miles);
And the exception I’m getting is:”Input string was not in a correct format.” for this line of code:
int oDistanceMiles = Convert.ToInt32(miles);
I’m lost. .Net 4.0 C#
What if you get rid of the ” mi” first, and treat the rest as a float? Then you can use the Math.Round to round to a whole number and convert that to an int.
If I’m right, the NumberStyles.Float enum will automatically clean up leading or trailing white space. Of course, you can also use string.Trim()