打印字符串和格式化变量

时间:2017-03-05 04:56:05

标签: fortran

考虑以下计划

program foo
    implicit none
    real :: pi
    pi = 3.1415923
    print*, "The value of pi is", pi
end program foo

这使输出为

The value of pi is 3.1415923

但我希望变量最多只显示2个小数位,所以我试过

print'(f4.2)', "The value of pi is", pi

这会引发一个错误。

Fortran runtime error: Expected REAL for item 1 in formatted transfer, got CHARACTER                                                                                                                                       
(f4.2)  

如何实现以下输出

The value of pi is 3.14

1 个答案:

答案 0 :(得分:0)

我使用以下

实现了所需的输出
write(*,"(A)",advance="no") "The value of pi is " 
write(*,'(f4.2)') pi

print为(感谢高性能标记)

print'(a,f5.2)', "The value of pi is", pi