I am writing an output to a file that requires a strict format that is the Fortran Format 2X, 6G13.6. Has anyone here coded in fortran in the past and know how to reproduce in .net?
According to the information that I obtained
here, the format with descriptor Gw.d where w is the total field width and d is the number of digits to the right of the decimal point creates an output where if the value is greater than 0.1 but not too large to fit in the field then the number is written as if in decimal format (similar to the F descriptor) using a field width of w and outputting d significant digits. This number is followed by four blanks. Otherwise, it behaves exactly like the Ew.d descriptor. The 2X just means start the line with 2 blank spaces and the 6 preceeding the G just means to output 6 values in the G13.6 format.
I have already played around with it too much today and my head is about to explode. Does anyone know of any project that has recreated something like this before? I have started some code below but I have a feeling that I am approaching it in a very wrong way.
public static string WriteFormat(double value)
{
// Special output condition when value = 0
if (value == 0)
{
return "0.000000".PadLeft(13);
}
// Maximum field width larger than max int so check against max int
if (value > .1 && value < 999999999999)
{
string temp = value.ToString();
// First check if the number does not have a decimal
if (temp.IndexOf(".") == -1)
{
var length = temp.Length;
if (value <1000000)
{
if (length >= 6)
{
return (value.ToString("G6") + " ").PadLeft(13);
}
var pad = 7 - length;
return (value.ToString() + ".".PadRight(pad, '0')+ " ").PadLeft(13);
}
}
}
return value.ToString("#.####0.0e+00").PadLeft(13);
}
What Output Should look like