[英]Converting arbitrary floating-point string to real in Fortran 95
Are there any easy ways of convertin an arbitrary floating-point string to a real number in fortran? 有没有简单的方法将任意浮点字符串转换为fortran中的实数? Think of something like
strtod
? 想想像
strtod
这样的东西? The problem with READ
statement is that all floating-point formats edit descriptors require explicit widths. READ
语句的问题是所有浮点格式编辑描述符都需要显式宽度。 So far the best workaround I made is something like: 到目前为止,我所做的最好的解决方法是:
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
But I'm wondering if I'm missing something and may be is there a better way to do that? 但我想知道我是否遗漏了某些东西,可能有更好的方法吗?
I must be missing something. 我肯定错过了什么。 What's wrong with doing it with list directed?
使用列表定向执行此操作有什么问题?
[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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.