NAME
aton(3f) - [M_strings:TYPE] function returns argument as a numeric
value from a string
(LICENSE:PD)
SYNOPSIS
logical function aton(str,val[,msg])
character(len=*),intent(in) :: str
type(TYPE(kind=KIND)),intent(out) :: val
character(len=:),allocatable,intent(out) :: msg
DESCRIPTION
This function converts a string to a numeric value.
OPTIONS
str holds string assumed to represent a numeric value
val returned value. May be REAL or INTEGER.
msg message describing error when ATON returns .false.
RETURNS
aton .true. if the conversion was successful, .false. otherwise
EXAMPLE
Sample Program:
program demo_aton
use M_strings, only: aton
implicit none
character(len=14),allocatable :: strings(:)
doubleprecision :: dv
integer :: iv
real :: rv
integer :: i
! 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
do i=1,size(strings)
write(*,'(a)',advance='no')'STRING:',strings(i)
if(aton(strings(i),iv)) write(*,'(g0)',advance='no')':INTEGER ',iv
if(aton(strings(i),rv)) write(*,'(g0)',advance='no')':INTEGER ',rv
if(aton(strings(i),dv)) write(*,'(g0)',advance='no')':INTEGER ',dv
enddo
end program demo_aton
AUTHOR
LICENSE
Arguments
Type |
Intent | Optional | Attributes |
|
Name |
|
character(len=*),
|
intent(in) |
|
|
:: |
str |
|
real(kind=wp)
|
|
|
|
:: |
val |
|
character(len=:),
|
intent(out), |
optional, |
allocatable
|
:: |
msg |
|
Return Value
logical