string_to_integer Subroutine

public subroutine string_to_integer(str, ival, status_ok)

Convert a string into an integer.


  • Jacob Williams : 12/10/2013 : Rewrote original parse_integer routine. Added error checking.
  • Modified by Izaak Beekman
  • Jacob Williams : 2/4/2017 : moved core logic to this routine.


Type IntentOptional AttributesName
character(kind=CK,len=*), intent(in) :: str

the string to convert to an integer

integer(kind=IK), intent(out) :: ival

the integer value

logical(kind=LK), intent(out) :: status_ok

true if there were no errors

Called by

proc~~string_to_integer~~CalledByGraph proc~string_to_integer string_to_integer proc~json_get_by_path_rfc6901 json_get_by_path_rfc6901 proc~json_get_by_path_rfc6901->proc~string_to_integer proc~json_get_by_path_jsonpath_bracket json_get_by_path_jsonpath_bracket proc~json_get_by_path_jsonpath_bracket->proc~string_to_integer proc~string_to_int string_to_int proc~string_to_int->proc~string_to_integer proc~json_get_integer json_get_integer proc~json_get_integer->proc~string_to_integer


Source Code

Source Code

    subroutine string_to_integer(str,ival,status_ok)

    implicit none

    character(kind=CK,len=*),intent(in) :: str        !! the string to convert to an integer
    integer(IK),intent(out)             :: ival       !! the integer value
    logical(LK),intent(out)             :: status_ok  !! true if there were no errors

    character(kind=CDK,len=:),allocatable :: digits
    integer(IK) :: ndigits_digits,ndigits,ierr

    ! Compute how many digits we need to read
    ndigits = 2*len_trim(str)
    ndigits_digits = floor(log10(real(ndigits)))+1
    allocate(character(kind=CDK,len=ndigits_digits) :: digits)
    write(digits,'(I0)') ndigits !gfortran will have a runtime error with * edit descriptor here
    ! gfortran bug: '*' edit descriptor for ISO_10646 strings does bad stuff.
    read(str,'(I'//trim(digits)//')',iostat=ierr) ival   !string to integer

    ! error check:
    status_ok = (ierr==0)
    if (.not. status_ok) ival = 0_IK

    end subroutine string_to_integer