c# - Unexpected exception thrown by Convert.ToDecimal -


this old 1 can't find on it. have idea why this:

convert.todecimal("3.14521963414679e-08") 

throws

formatexception ("input string not in correct format.")

however works expected?

convert.todouble("3.14521963414679e-08") 

i thought convert.todecimal handle exponentials - maybe got wrong.

convert.todecimal doesn't support scientific notation.

it documented convert.todecimal internally uses decimal.parse , documentation decimal.parse states uses numberstyles.number , following valid input:

[ws][sign][digits,]digits[.fractional-digits][ws]  

to support scientific notation have use overload of decimal.parse allows specify numberstyles used:

var result = decimal.parse("3.14521963414679e-08",                            numberstyles.number | numberstyles.allowexponent); 

Comments