C Library Functions - isnumber (3)
NAME
isnumber(3f) - [M_strings:TYPE] determine if a string represents a number
(LICENSE:PD)
CONTENTS
Synopsis
Description
Options
Returns
Examples
Author
License
SYNOPSIS
function isnumber(str,msg)
character(len=*),intent(in) :: str
character(len=:),intent(out),allocatable,optional :: msg
DESCRIPTION
ISNUMBER(3f) returns a value greater than zero if the string represents
a number, and a number less than or equal to zero if it is a bad number.
Blank characters are ignored.
OPTIONS
|
str |
the string to evaluate as to whether it represents a numeric value
or not
|
|
msg |
An optional message describing the string
|
|
RETURNS
|
isnumber |
| |
the following values are returned
|
|
1 for an integer | | |
[-+]NNNNN
|
|
2 for a whole number | | |
[-+]NNNNN.
|
|
3 for a real value | | |
[-+]NNNNN.MMMM
|
|
4 for a exponential value | | |
[-+]NNNNN.MMMM[-+]LLLL
[-+]NNNNN.MMMM[ed][-+]LLLL
|
|
|
|
values less than 1 represent an error
EXAMPLES
As the example shows, you can use an internal READ(3f) along with the
IOSTAT= parameter to check (and read) a string as well.
program demo_isnumber
use M_strings, only : isnumber
implicit none
character(len=256) :: line
real :: value
integer :: ios1, ios2
integer :: answer
character(len=256) :: message
character(len=:),allocatable :: description
write(*,*)Begin entering values, one per line
do
read(*,(a),iostat=ios1)line
!
! try string as number using list-directed input
line=
read(line,*,iostat=ios2,iomsg=message) value
if(ios2 == 0)then
write(*,*)VALUE=,value
elseif( is_iostat_end(ios1) ) then
stop end of file
else
write(*,*)ERROR:,ios2,trim(message)
endif
!
! try string using isnumber(3f)
answer=isnumber(line,msg=description)
if(answer > 0)then
write(*,*) &
& for ,trim(line), ,answer,:,description
else
write(*,*) &
& ERROR for ,trim(line), ,answer,:,description
endif
!
enddo
end program demo_isnumber
Example run
> Begin entering values
> ERROR: -1 End of file
> ERROR for -1 :null string
>10
> VALUE= 10.0000000
> for 10 1 :integer
>20
> VALUE= 20.0000000
> for 20 1 :integer
>20.
> VALUE= 20.0000000
> for 20. 2 :whole number
>30.1
> VALUE= 30.1000004
> for 30.1 3 :real number
>3e1
> VALUE= 30.0000000
> for 3e1 4 :value with exponent
>1-2
> VALUE= 9.99999978E-03
> for 1-2 4 :value with exponent
>100.22d-4
> VALUE= 1.00220004E-02
> for 100.22d-4 4 :value with exponent
>1--2
> ERROR: 5010 Bad real number in item 1 of list input
> ERROR for 1--2 -5 :bad number
>e
> ERROR: 5010 Bad real number in item 1 of list input
> ERROR for e -6 :missing leading value before exponent
>e1
> ERROR: 5010 Bad real number in item 1 of list input
> ERROR for e1 -6 :missing leading value before exponent
>1e
> ERROR: 5010 Bad real number in item 1 of list input
> ERROR for 1e -3 :missing exponent
>1e+
> ERROR: 5010 Bad real number in item 1 of list input
> ERROR for 1e+ -4 :missing exponent after sign
>1e+2.0
> ERROR: 5010 Bad real number in item 1 of list input
> ERROR for 1e+2.0 -5 :bad number
AUTHOR
John S. Urban
LICENSE
Public Domain
| Nemo Release 3.1 | isnumber (3) | June 29, 2025 |
Generated by manServer 1.08 from bd1a695e-afde-438c-9710-3cf2b978988c using man macros.