简体   繁体   English

.NET格式说明符,用于科学计数法,尾数在0到1之间

[英].NET format specifier for scientific notation with mantissa between 0 and 1

I am working with a Fortran program that expects floating point numbers to be input using Fortran's E format specifier, which is scientific notation, except the mantissa must be between 0 and 1 . 我正在使用一个Fortran程序,该程序期望使用Fortran的E格式说明符输入浮点数,这是科学计数法,除了尾数必须在0到1之间 So instead of: 所以代替:

"3147.3" --> "3.1473E3",

it needs 它需要

"3147.3" --> "0.31473E4".

I am unable to modify the Fortran program, as it works with a few other programs that are also particular. 我无法修改Fortran程序,因为它可以与其他一些特别的程序一起使用。

It would appear that the C# E format string would give me the former. 看来C# E格式字符串会给我前者。 Is there any simple way to achieve the latter in C#? 有没有简单的方法可以在C#中实现后者?

You could specify a custom format like so. 您可以像这样指定自定义格式。

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

I think that you are solving a non-existent problem. 我认为您正在解决一个不存在的问题。 It is true that the default of the Fortran E output specifier has a leading zero before the decimal point (this can be modified). 的确,Fortran E 输出说明符的默认值在小数点之前有一个前导零(可以修改)。 But when the E specifier is used for input it is very tolerant and does not require the leading zero -- if you have a decimal point in the number and the number fits within the columns specified by the format, it will work. 但是,当使用E说明符进行输入时,它是非常宽容的,并且不需要前导零-如果数字中有小数点且该数字适合格式指定的列,则它将起作用。

Here is an example Fortran program, and an example input file. 这是一个示例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

and the sample input with three different cases: 以及具有三种不同情况的样本输入:

3.1473E3
0.31473E4
3147.3

I tested the program with gfortran and Intel ifort. 我使用gfortran和Intel ifort测试了该程序。 The output was: 输出为:

 3147.300       3147.300       3147.300

So when performing input using Fortran's E format specifier, it is not necessary that the digit before the decimal point be zero. 因此,在使用Fortran的E格式说明符执行输入时,小数点前的数字不必为零。 It is not even necessary that the input value use E-notation! 输入值甚至不必使用E表示法!

Edit / PS I translated the program to the fixed-form source layout of FORTRAN 77 and compiled it with g77 -- it read the three test numbers just fine. 编辑/ PS我将程序转换为FORTRAN 77的固定格式源布局,并使用g77进行了编译-它读取了三个测试编号就很好了。 The E-format has been flexible for input for a long time -- probably since FORTRAN IV, perhaps longer. E格式已经很灵活地输入很长时间了-可能是自FORTRAN IV起,甚至更长。

The representaiton of floats or doubles are defined in IEEE754 / IEC 60559:1989. 浮点数或双精度数的表示形式在IEEE754 / IEC 60559:1989中定义。 You should look to find libraries to extract mantissa and exponent. 您应该寻找找到提取尾数和指数的库。 Then you could just divide by then to move to comma and subtract the number of steps from the exponent to form your solution. 然后,您可以除以然后移至逗号,然后从指数中减去步数即可形成解决方案。

You could take something similar to Jeff M's solution, and implement it via extension method: 您可以采用类似于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);
    }
}

Or for something a little more complicated (not sure how much precision Fortran floats/doubles give): 或更复杂的东西(不确定Fortran浮点数/双精度数给出的精度):

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))
            ));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM