如何左对齐 Fortran 中的数字输出?
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有什么特别漂亮的方法。但是,使用内部 WRITE 语句将数字转换为文本字符串(以前使用 ENCODE 语句完成),然后操作文本可能会满足您的需要。
引用 http://rsusu1.rnd.runnet.ru/develop/fortran/prof77/node168.html
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
使用 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.
真正不优雅的是我的方法(我像穴居女人一样编程),在编写简单的 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
如果您真正想要的是输出字段之间的空格而不是左对齐数字以留下空格,您可以简单地使用
X
编辑描述符。例如,将在每个字段与下一个字段之间插入 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 examplewill 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-standardX
too.!对于具有 1 位小数的左对齐浮点数。小数点右侧的数字是需要多少位小数。自动将舍入写入所需的小数位,而不是截断。
!对于左对齐整数..
*在 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.
!for left-justified integers..
*after feedback from Vladimir, retesting proved the command works with or without the array brackets