有没有简单的方法将任意浮点字符串转换为fortran中的实数?想想strtod
之类的东西? READ
语句的问题是所有浮点格式编辑描述符都需要显式宽度。到目前为止,我所做的最好的解决方法是:
pure function strtod(s)
real(kind=8) :: strtod
character(len=*), intent(in) :: s
character(len=32) :: fmt
integer :: dot
dot = index(s, ".")
if(dot < 1) then
write(fmt, '("(F",I0,".0)")'), len_trim(s)
else
write(fmt, '("(F",I0,".",I0,")")'), len_trim(s), len_trim(s)-dot
end if
read(s,fmt), strtod
end function strtod
但我想知道我是否遗漏了某些东西,可能有更好的方法吗?
答案 0 :(得分:2)
我一定错过了什么。使用列表定向执行此操作有什么问题?
[luser@cromer stackoverflow]$ cat char2.f90
Program char2
Implicit None
Integer, Parameter :: wp = Selected_real_kind( 12, 70 )
Real( wp ) :: a
Character( Len = 32 ) :: s
s = '0.'
Read( s, * ) a
Write( *, * ) a
s = '1e10'
Read( s, * ) a
Write( *, * ) a
End Program char2
[luser@cromer stackoverflow]$ nagfor -C=all -C=undefined char2.f90
NAG Fortran Compiler Release 5.3.1 pre-release(904)
[NAG Fortran Compiler normal termination]
[luser@cromer stackoverflow]$ ./a.out
0.0000000000000000
1.0000000000000000E+10