s2v(3f) - [M_strings:TYPE] function returns doubleprecision
numeric value from a string
(LICENSE:PD)
function s2v(string[,ierr][,onerr])
character(len=*) :: string
doubleprecision :: s2v
integer,intent(out),optional :: ierr
class(*),intent(in),optional :: onerr
This function converts a string to a DOUBLEPRECISION numeric value.
The intrinsics INT(3f), REAL(3f), and DBLE(3f) are also extended
to take CHARACTER variables. The KIND= keyword is not supported
on the extensions.
string holds string assumed to represent a numeric value
ierr If an error occurs the program is stopped if the optional
parameter IERR is not present. If IERR returns a non-zero
value an error occurred.
onerr The value to return on error. A value of NaN is
returned on error by default.
s2v numeric value read from string
Sample Program:
program demo_s2v
use M_strings, only: s2v, int, real, dble
implicit none
character(len=8) :: s=' 10.345 '
integer :: i
character(len=14),allocatable :: strings(:)
doubleprecision :: dv
integer :: errnum
! different strings representing INTEGER, REAL, and DOUBLEPRECISION
strings=[&
&' 10.345 ',&
&'+10 ',&
&' -3 ',&
&' -4.94e-2 ',&
&'0.1 ',&
&'12345.678910d0',&
&' ',& ! Note: will return zero without an error message
&'1 2 1 2 1 . 0 ',& ! Note: spaces will be ignored
&'WHAT? '] ! Note: error messages will appear, zero returned
! a numeric value is returned,
! so it can be used in numeric expression
write(*,*) '1/2 value of string is ',s2v(s)/2.0d0
write(*,*)
write(*,*)' STRING VALUE ERROR_NUMBER'
do i=1,size(strings)
! Note: not a good idea to use s2v(3f) in a WRITE(3f) statement,
! as it does I/O when errors occur, so called on a separate line
dv=s2v(strings(i),errnum)
write(*,*) strings(i)//'=',dv,errnum
enddo
write(*,*)"Extended intrinsics"
write(*,*)'given inputs:',s,strings(:8)
write(*,*)'INT(3f):',int(s),int(strings(:8))
write(*,*)'REAL(3f):',real(s),real(strings(:8))
write(*,*)'DBLE(3f):',dble(s),dble(strings(:8))
write(*,*)"That's all folks!"
end program demo_s2v
Expected output
>1/2 value of string is 5.1725000000000003
>
> STRING VALUE ERROR_NUMBER
> 10.345 = 10.345000000000001 0
>+10 = 10.000000000000000 0
> -3 = -3.0000000000000000 0
> -4.94e-2 = -4.9399999999999999E-002 0
>0.1 = 0.10000000000000001 0
>12345.678910d0= 12345.678910000001 0
> = 0.0000000000000000 0
>1 2 1 2 1 . 0 = 12121.000000000000 0
>*a2d* - cannot produce number from string [WHAT?]
>*a2d* - [Bad value during floating point read]
>WHAT? = 0.0000000000000000 5010
>Extended intrinsics
>given inputs: 10.345 10.345 +10 -3 -4.94e-2 0.1
12345.678910d0 1 2 1 2 1 . 0
>INT(3f): 10 10 10 -3 0 0 12345 0 12121
>REAL(3f): 10.3450003 10.3450003 10.0000000 -3.00000000
-4.94000018E-02
> 0.100000001 12345.6787 0.00000000 12121.0000
>DBLE(3f): 10.345000000000001 10.345000000000001
10.000000000000000
> -3.0000000000000000 -4.9399999999999999E-002
0.10000000000000001
> 12345.678910000001 0.0000000000000000
12121.000000000000
>That's all folks!
John S. Urban
Public Domain
DESCRIPTION: s2v(3f): function returns doubleprecision number from string;zero if error occurs
AUTHOR: John S. Urban
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | chars | |||
integer, | optional | :: | ierr | |||
class(*), | intent(in), | optional | :: | onerr |
doubleprecision function s2v(chars,ierr,onerr)
! 1989 John S. Urban
! ident_52="@(#) M_strings s2v(3f) returns doubleprecision number from string;zero if error occurs"
character(len=*),intent(in) :: chars
integer,optional :: ierr
doubleprecision :: valu
integer :: ierr_local
class(*),intent(in),optional :: onerr
ierr_local=0
if(present(onerr))then
call a2d(chars,valu,ierr_local,onerr)
else
call a2d(chars,valu,ierr_local)
endif
if(present(ierr))then ! if error is not returned stop program on error
ierr=ierr_local
s2v=valu
elseif(ierr_local /= 0)then
write(*,*)'*s2v* stopped while reading '//trim(chars)
stop 1
else
s2v=valu
endif
end function s2v