如何使用 C# 解析科学格式的双精度数
我从 FORTRAN 程序中输出了以下格式的数字:
0.12961924D+01
如何使用 C# 将其解析为双精度?
我尝试了以下方法但没有成功:
// note leading space, FORTRAN pads its output so that positive and negative
// numbers are the same string length
string s = " 0.12961924D+01";
double v1 = Double.Parse(s)
double v2 = Double.Parse(s, NumberStyles.Float)
I have numbers outputted from a FORTRAN program in the following format:
0.12961924D+01
How can I parse this as a double using C#?
I have tried the following without success:
// note leading space, FORTRAN pads its output so that positive and negative
// numbers are the same string length
string s = " 0.12961924D+01";
double v1 = Double.Parse(s)
double v2 = Double.Parse(s, NumberStyles.Float)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我首先会对此字符串进行一些操作,将其从 FORTRAN 格式转换为 .NET 格式:
以下内容应该可以满足您的需求:
I would first do some manipulation of this string to get it from FORTRAN to .NET formatting:
The below should get you what you need:
这应该有帮助:
s = s.Replace(' ', '-').Replace('D', 'E');
This should help:
s = s.Replace(' ', '-').Replace('D', 'E');
由于其他人都建议用减号替换空格,这看起来很疯狂,所以我将提供这个稍微简单的解决方案:
Since everyone else suggests replacing the space with a minus sign, which seems crazy, I'll offer this somewhat simpler solution: