.NET format specifier for scientific notation with mantissa from 0 to 1

I am working with a Fortran program that expects input of floating point numbers using the Fortran format specifier E, which is a scientific note, except for the mantissa should be between 0 and 1 . Therefore, instead of:

"3147.3" --> "3.1473E3",

he needs

"3147.3" --> "0.31473E4".

I cannot change the Fortran program, since it works with several other programs that are also especially important.

It seems like a C # format string Ewould give me the first. Is there an easy way to achieve the latter in C #?

+3
source share
4 answers

You can specify your own format.

var num = 3147.3;
num.ToString("\\0.#####E0"); // "0.31473E4"
+3

, . , Fortran E ( ). E , - , , , .

Fortran .

program test_format

real :: num1, num2, num3

open (unit=16, file="numbers_3.txt", status='old', access='sequential', form='formatted', action='read' ) 

read (16, 1010 ) num1
read (16, 1010 ) num2
read (16, 1010 ) num3

1010 format (E9.5)

write (*, *) num1, num2, num3

stop

end program test_format

:

3.1473E3
0.31473E4
3147.3

gfortran Intel ifort. :

 3147.300       3147.300       3147.300

Fortran E , . , E-!

/P.S. FORTRAN 77 g77 - . E- - , FORTRAN IV, , .

+2

IEEE754/IEC 60559: 1989. . , , .

0

- Jeff M :

public static class DoubleExtensions
{
    public static string ToFortranDouble(this double value)
    {
        return value.ToString("\\0.#####E0");
    }
}

class Program
{
    static void Main(string[] args)
    {
        string fortranValue = 3147.3.ToFortranDouble();
        System.Console.WriteLine(fortranValue);
    }
}

- ( , / ):

public static class DoubleExtensions
{
    public static string ToFortranDouble(this double value)
    {
        return value.ToFortranDouble(4);
    }

    public static string ToFortranDouble(this double value, int precision)
    {
        return string.Format(value.ToString(
            string.Format("\\0.{0}E0", new string('#', precision))
            ));
    }
}
0

Source: https://habr.com/ru/post/1759806/


All Articles