TheSwamp

Code Red => .NET => Topic started by: Keith Brown on October 02, 2015, 04:42:03 PM

Title: Easy way to create 2X, 6G13.6 Fortran Format in C#
Post by: Keith Brown on October 02, 2015, 04:42:03 PM
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 (http://www.obliquity.com/computer/fortran/format.html), 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.


Code - C#: [Select]
  1. public static string WriteFormat(double value)
  2. {
  3.         // Special output condition when value = 0
  4.    if (value == 0)
  5.    {
  6.       return "0.000000".PadLeft(13);
  7.    }
  8.  
  9.  
  10.    // Maximum field width larger than max int so check against max int
  11.    if (value > .1 && value < 999999999999)
  12.    {
  13.       string temp = value.ToString();
  14.            
  15.                 // First check if the number does not have a decimal
  16.       if (temp.IndexOf(".") == -1)
  17.       {
  18.          var length = temp.Length;
  19.          if (value <1000000)
  20.          {
  21.             if (length >= 6)
  22.             {
  23.                return (value.ToString("G6") + "    ").PadLeft(13);
  24.             }
  25.             var pad = 7 - length;
  26.             return (value.ToString() + ".".PadRight(pad, '0')+ "    ").PadLeft(13);
  27.          }
  28.       }
  29.    }
  30.    return value.ToString("#.####0.0e+00").PadLeft(13);
  31. }


What Output Should look like

(https://farm6.staticflickr.com/5691/21707335858_cc00624d4a_b.jpg) (https://flic.kr/p/z5cLFm)
Title: Re: Easy way to create 2X, 6G13.6 Fortran Format in C#
Post by: MexicanCustard on October 05, 2015, 10:36:10 AM
If memory serves me correctly, 6G13.6.  I have long forgotten Fortran and Cobal.

G formating, (.6) use 6 significant digits, (6 in front of G) pad right hand side with 6 spaces, (13) total width/pad front to make this many total spaces.

Do you need to cover multiple format types or just the 'G' format? Is the formatting variable or is the output always going to follow a single specification?
Title: Re: Easy way to create 2X, 6G13.6 Fortran Format in C#
Post by: Keith Brown on October 05, 2015, 11:04:03 AM
I really only need it for the G as 95% of them are all the (2X, 6G13.6) format.  I could be wrong but the way that I read the specification is that 2X signifies that the first two characters of the line are spaces and then the G13.6 format repeats 6 times.  There is a maximum of 13 digits and there are 6 significant digits.  So a value of 10 would be 10.0000 and a value of 190 would be 190.000.  There are other requirements in the specification also. 


I think i just need to come up with the individual cases and then write the code for each.  I usually get confused when trying to do something like this however and thought maybe someone had encountered it before and had a ready made solution.