如何左对齐 Fortran 中的数字输出?

发布于 2024-10-09 11:51:35 字数 397 浏览 6 评论 0原文

我正在用 Fortran 编写一些简单的输出,但我想要空格分隔符。但是,如果使用以下语句:

format(A20,ES18.8,A12,ES18.8)

我得到这样的输出:

p001t0000               3.49141273E+01obsgp_oden      1.00000000E+00

我更喜欢这样:

p001t0000           3.49141273E+01   obsgp_oden  1.00000000E+00

我尝试使用负值作为宽度(就像在Python中一样)但没有骰子。那么,有没有办法让数字左对齐呢?

非常感谢!

I am writing some simple output in fortran, but I want whitespace delimiters. If use the following statement, however:

format(A20,ES18.8,A12,ES18.8)

I get output like this:

p001t0000               3.49141273E+01obsgp_oden      1.00000000E+00

I would prefer this:

p001t0000           3.49141273E+01   obsgp_oden  1.00000000E+00

I tried using negative values for width (like in Python) but no dice. So, is there a way to left-justify the numbers?

Many thanks in advance!

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

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

发布评论

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

评论(5

又怨 2024-10-16 11:51:35

没有什么特别漂亮的方法。但是,使用内部 WRITE 语句将数字转换为文本字符串(以前使​​用 ENCODE 语句完成),然后操作文本可能会满足您的需要。

引用 http://rsusu1.rnd.runnet.ru/develop/fortran/prof77/node168.html

内部文件 WRITE 通常是
用于将数值转换为
字符串通过使用合适的
格式规范,例如:

<前><代码> 字符*8 CVAL
右值 = 98.6
WRITE(CVAL, '(SP, F7.2)') RVALUE

WRITE 语句将填充
字符变量 CVAL 与
字符“+98.60”(请注意,有
两端各有一个空白
数字,第一个,因为数字
在 7 域中右对齐
字符,第二个是因为
记录被填充到声明的
长度为 8 个字符)。

一旦一个数字变成了
可以处理的字符串
进一步以所描述的各种方式
在第 7 节中。这使得有可能,
例如,写数字
在字段中左对齐,...

There's not a particularly beautiful way. However, using an internal WRITE statement to convert the number to a text string (formerly done with an ENCODE statement), and then manipulating the text may do what you need.

Quoting http://rsusu1.rnd.runnet.ru/develop/fortran/prof77/node168.html

An internal file WRITE is typically
used to convert a numerical value to a
character string by using a suitable
format specification, for example:

  CHARACTER*8 CVAL 
  RVALUE = 98.6 
  WRITE(CVAL, '(SP, F7.2)') RVALUE

The WRITE statement will fill the
character variable CVAL with the
characters ' +98.60 ' (note that there
is one blank at each end of the
number, the first because the number
is right-justified in the field of 7
characters, the second because the
record is padded out to the declared
length of 8 characters).

Once a number has been turned into a
character-string it can be processed
further in the various ways described
in section 7. This makes it possible,
for example, to write numbers
left-justified in a field, ...

背叛残局 2024-10-16 11:51:35

使用 Fortran 95 更容易做到这一点,但仍然不简单。使用 write 语句将数字或其他项目写入字符串(如第一个答案中所示)。然后使用 Fortran 95 内在的“ADJUSTL”向左调整字符串的非空白字符。

This is easier with Fortran 95, but still not trivial. Write the number or other item to a string with a write statement (as in the first answer). Then use the Fortran 95 intrinsic "ADJUSTL" to left adjust the non-blank characters of the string.

甜点 2024-10-16 11:51:35

真正不优雅的是我的方法(我像穴居女人一样编程),在编写简单的 Fortran 写入格式(不是 LJ)后,我使用 Excel(csv)和 ultraedit 的组合来删除空格,有效地获得所需的结果LJ 后面直接跟逗号(我需要将其用于另一个软件的特定导入格式)。 BF

And really un-elegant is my method (I program like a cave woman), after writing the simple Fortran write format (which is not LJ), I use a combination of Excel (csv) and ultraedit to remove the spaces effectively getting the desired LJ followed directly by commas (which I need for my specific import format to another software). BF

初懵 2024-10-16 11:51:35

如果您真正想要的是输出字段之间的空格而不是左对齐数字以留下空格,您可以简单地使用 X 编辑描述符。例如,

format(A20,4X,ES18.8,4X,A12,4X,ES18.8)

将在每个字段与下一个字段之间插入 4 个空格。请注意,标准要求 1X 代表一个空格,当前的一些编译器也接受非标准 X

If what you really want is whitespace between output fields rather than left-justified numbers to leave whitespace you could simply use the X edit descriptor. For example

format(A20,4X,ES18.8,4X,A12,4X,ES18.8)

will insert 4 spaces between each field and the next. Note that the standard requires 1X for one space, some of the current compilers accept the non-standard X too.

ζ澈沫 2024-10-16 11:51:35

!对于具有 1 位小数的左对齐浮点数。小数点右侧的数字是需要多少位小数。自动将舍入写入所需的小数位,而不是截断。

write(*, ['(f0.1)']) RValue !or
write(*, '(f0.1)') RValue 

!对于左对齐整数..

write(*, ['(i0)']) intValue !or
write(*, '(i0)') RValue 

*在 Vladimir 反馈后,重新测试证明该命令在有或没有数组括号的情况下都可以工作

!for left-justified float with 1 decimal.. the number to the right of the decimal is how many decimals are required. Write rounds to the desired decimal space automatically, rather than truncating.

write(*, ['(f0.1)']) RValue !or
write(*, '(f0.1)') RValue 

!for left-justified integers..

write(*, ['(i0)']) intValue !or
write(*, '(i0)') RValue 

*after feedback from Vladimir, retesting proved the command works with or without the array brackets

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