li913 发表于 2017-3-9 17:40
假如你限定宽度为7,怎么输出 123456.789 ?
program test
implicit none
integer::n=7,m,i
character(40):: str='',strF="(f20.20)"
real a,b
!设置输出格式
write(strF(3:4),'(i2.2)') n*2+1
write(strF(6:7),'(i2.2)') n
!输出到字符串
a= 1234567
write(str,strf) a
str=adjustl(str)
!验证
if(str(1:1)=='*') then
write(*,*) '输出域宽不够'
else
read(str(1:n),*) b
str(n+1:) = ''
if(abs(a-b)>1.0) then
write(*,*) '输出域宽不够'
else
write(*,"(a)") str(1:n)
end if
end if
pause
end program
li913 发表于 2017-3-10 12:39
[mw_shl_code=fortran,true]program test
implicit none
integer::n=7,m,i
欢迎光临 Fortran Coder (http://bbs.fcode.cn/) | Powered by Discuz! X3.2 |