在 C# 中从科学记数法字符串转换为浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/64639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert from scientific notation string to float in C#
提问by odbasta
What's the proper way to convert from a scientific notation string such as "1.234567E-06" to a floating point variable using C#?
使用 C# 从科学记数法字符串(例如“1.234567E-06”)转换为浮点变量的正确方法是什么?
采纳答案by odbasta
Double.Parse("1.234567E-06", System.Globalization.NumberStyles.Float);
回答by Jaymie Thomas
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.
MyFloat
如果由于某种原因无法执行转换,这将确保将其设置为值 0。或者您可以将Double.Parse()
示例包装在一个Try..Catch
块中,并MyFloat
在检测到异常时设置为您选择的值。