I want to parse a string to long the value is 1.0010412473392E+15.But it gives a exception input string was not in a correct format.how to do this.
Both these answers work how to select both of them as answer.
Check out the System.Globalization.NumberStyles
enumeration in the appropriate overload of Int64.Parse. If you specify System.Globalization.NumberStyles.Any
, it should work:
long v = Int64.Parse(s, System.Globalization.NumberStyles.Any);
Note, however that the number you are parsing has limited precision, (there are only 13 decimal places but is specified as E+15). Also, the 'Any' enumeration is probably more than you really need - in this case you only need AllowDecimalPoint
and AllowExponent
:
long v = Int64.Parse(s, System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowExponent);
Are you sure you don't want to parse to double
?
var myDouble = double.Parse(myString);
You can then try converting to long
.
var myLong = Convert.ToInt64(myDouble);