简体   繁体   English

从科学记数法字符串转换为C#中的float

[英]Convert from scientific notation string to float in C#

使用C#将科学记数法字符串(如“1.234567E-06”)转换为浮点变量的正确方法是什么?

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

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. 如果由于某种原因无法执行转换,这将确保将MyFloat设置为值0。 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. 或者,您可以在Try..Catch块中包装Double.Parse()示例,并在检测到异常时将MyFloat设置为您选择的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM