Convert a string into a real(RK)
.
fmt=*
, rather than
fmt=real_fmt
, since it doesn’t work for some unusual cases
(e.g., when str='1E-5'
).Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(kind=CK,len=*), | intent(in) | :: | str | the string to convert to a real |
||
real(kind=RK), | intent(out) | :: | rval |
|
||
logical(kind=LK), | intent(out) | :: | status_ok | true if there were no errors |
subroutine string_to_real(str,rval,status_ok)
implicit none
character(kind=CK,len=*),intent(in) :: str !! the string to convert to a real
real(RK),intent(out) :: rval !! `str` converted to a real value
logical(LK),intent(out) :: status_ok !! true if there were no errors
integer(IK) :: ierr !! read iostat error code
read(str,fmt=*,iostat=ierr) rval
status_ok = (ierr==0)
if (.not. status_ok) rval = 0.0_RK
end subroutine string_to_real