s2v(3f) - [M_strings:TYPE] function returns doubleprecision numeric value from a string (LICENSE:PD)
Synopsis
Description
Options
Returns
Examples
Author
License
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_s2vuse 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(*,*)"Thats 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 >Thats all folks!
John S. Urban
Public Domain
Nemo Release 3.1 | s2v (3m_strings) | January 10, 2025 |