How to parse a double in scientific format using C #

I have numbers displayed from the FORTRAN program in the following format:

 0.12961924D+01

How can I parse this as double using C #?

I 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)
+5
source share
3 answers

First, I would do some manipulation of this string to get it from FORTRAN to .NET.

  • Trim any leading space; if the negative sign is there, we want it, but we do not want spaces.
  • Replace “D” with “E”.

Below you will get what you need:

string s = " 0.12961924D+01";
s = s.Trim().Replace("D", "E");
//s should now look like "0.12961924E01"    
double v2 = Double.Parse(s, NumberStyles.Float);
+7
source

This should help: s = s.Replace(' ', '-').Replace('D', 'E');

+1
source

Since everyone else suggests replacing space with a minus sign, which seems crazy, I suggest this is a slightly simpler solution:

string input = " 0.12961924D+01";
double output = Double.Parse(s.Replace('D', 'E'));
+1
source

All Articles