Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character, | intent(in) | :: | str | |||
real(kind=R8P), | intent(in) | :: | knd | |||
character, | intent(in), | optional | :: | pref | ||
integer(kind=I4P), | intent(out), | optional | :: | error |
function ctor_R8P(str, knd, pref, error) result(n)
!< Convert string to real.
!<
!<```fortran
!< use penf
!< print FR8P, cton(str='-1.0', knd=1._R8P)
!<```
!=> -0.100000000000000E+001 <<<
character(*), intent(in) :: str !< String containing input number.
real(R8P), intent(in) :: knd !< Number kind.
character(*), optional, intent(in) :: pref !< Prefixing string.
integer(I4P), optional, intent(out) :: error !< Error trapping flag: 0 no errors, >0 error occurs.
real(R8P) :: n !< Number returned.
integer(I4P) :: err !< Error trapping flag: 0 no errors, >0 error occurs.
character(len=:), allocatable :: prefd !< Prefixing string.
read(str, *, iostat=err) n ! Casting of str to n.
if (err/=0) then
prefd = '' ; if (present(pref)) prefd = pref
write(stderr, '(A,I1,A)') prefd//' Error: conversion of string "'//str//'" to real failed! real(', kind(knd), ')'
endif
if (present(error)) error = err
endfunction ctor_R8P