Convert from scientific notation string to float in C#

五迷三道 提交于 2019-12-17 06:14:40

问题


What's the proper way to convert from a scientific notation string such as "1.234567E-06" to a floating point variable using C#?


回答1:


Double.Parse("1.234567E-06", System.Globalization.NumberStyles.Float);



回答2:


Also consider using

Double.TryParse("1.234567E-06", System.Globalization.NumberStyles.Float, out MyFloat);

This will ensure that MyFloat is set to value 0 if, for whatever reason, the conversion could not be performed. Or you could wrap the Double.Parse() example in a Try..Catch block and set MyFloat to a value of your choosing when an exception is detected.



来源:https://stackoverflow.com/questions/64639/convert-from-scientific-notation-string-to-float-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!