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

发布于 2024-09-15 03:28:57 字数 350 浏览 2 评论 0原文

我正在使用一个 Fortran 程序,该程序期望使用 Fortran 的 E 格式说明符输入浮点数,这是科学记数法,除了尾数必须在 0 和 1 之间。因此,而不是:

"3147.3" --> "3.1473E3",

它需要

"3147.3" --> "0.31473E4".

我无法修改 Fortran 程序,因为它可以与其他一些也很特殊的程序一起使用。

看来 C# E 格式字符串会给我前者。有没有简单的方法可以在 C# 中实现后者?

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. 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.

It would appear that the C# E format string would give me the former. Is there any simple way to achieve the latter in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

灼痛 2024-09-22 03:28:57

您可以像这样指定自定义格式。

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

You could specify a custom format like so.

var num = 3147.3;
num.ToString("\\0.#####E0"); // "0.31473E4"
尽揽少女心 2024-09-22 03:28:57

我认为你正在解决一个不存在的问题。确实,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 符号!

编辑/ PS 我将程序翻译为 FORTRAN 77 的固定格式源布局,并用 g77 编译它——它很好地读取了三个测试数字。 E 格式长期以来一直具有灵活的输入功能——可能是从 FORTRAN IV 开始,也许更久。

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). 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.

Here is an example Fortran program, and an example input file.

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. 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. It is not even necessary that the input value use E-notation!

Edit / P.S. 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. The E-format has been flexible for input for a long time -- probably since FORTRAN IV, perhaps longer.

灼疼热情 2024-09-22 03:28:57

浮点数或双精度数的表示在 IEEE754 / IEC 60559:1989 中定义。您应该寻找库来提取尾数和指数。然后你可以除以 then 移动到逗号并从指数中减去步数来形成你的解决方案。

The representaiton of floats or doubles are defined in 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.

-黛色若梦 2024-09-22 03:28:57

您可以采用类似于 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);
    }
}

或者对于更复杂的东西(不确定 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))
            ));
    }
}

You could take something similar to Jeff M's solution, and implement it via extension method:

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):

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))
            ));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文