Skip to main content
Engineering LibreTexts

19.1: Character String to Numeric Conversion

  • Page ID
    54384
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    A character string can be converted into an integer or real value using an internal read operation. The string is provided as the input instead of a file unit number. The numeric variable is provided as the location for the result of the read operation. The format will provide guidance for the conversion.

    The following is a simple example that will declare two strings and convert the first into an integer value and the second into a real value. Additionally, a third string conversion is perform on an invalid numeric string (to better show the error handling).

    ! Example program to use an internal read for
    ! character / numeric conversion.
    
    program cvtExample 1
    
    implicit none
    integer :: cvtErr
    character(4) :: iString = "1234"
    character(7) :: rString = "3.14159"
    character(7) :: badString = "3.14z59"
    integer :: iNum1, iNum2
    real :: pi, tau
    
        write (*,'(a, /)') "Example Conversion Program."
    
    ! ----------
    !  Convert string to an integer value.
    
        read (iString, '(i10)', iostat=cvtErr) iNum1
    
        if (cvtErr == 0 ) then
            iNum2 = iNum1 * 2
            write (*,'(a, i5, /, a, i5, /)')            &
                "num1 = ", iNum1, "num2 = ", iNum2
        else
            write (*,'(a, /)') "Error, invalid integer string."
        end if
    
    ! ----------
    !  Convert string to a real value.
    
        read (rString, '(f17.6)', iostat=cvtErr) pi
        
        if (cvtErr == 0 ) then
            tau = pi * 2.0
            write (*,'(a, f5.3, /, a, f5.3, /)')        &
                "pi = ", pi, "tau = ", tau
        else
            write (*,'(a, /)') "Error, invalid real string."
        end if
    
    ! ----------
    !  Convert string to a real value.
    
        read (badString, '(f12.4)', iostat=cvtErr) pi
        
        if (cvtErr == 0 ) then
            tau = pi * 2.0
            write (*,'(a, f5.3, /, a, f6.3)')           &
                "pi = ", pi, "tau = ", tau
        else
            write (*,'(a, /)') "Error, invalid real string."
        end if
    
    end program cvtExample1
    

    The specific formats used on the read operations in the example are wider or larger than the expected number (which is allowed). Should a smaller format be used, it would either truncate the value or possibly generate a conversion error. To ensure appropriate conversion, the final values should be verified against the expected result.

    An example of the output for this program is as follows:

    Example Conversion Program.
    
    num1 =  1234
    num2 =  2468
    
    pi = 3.142
    tau = 6.283
    
    Error, invalid real string.
    

    The multiplication by 2 for each of the numeric values was performed only as an example since multiplication can only be performed on numeric data types (i.e., integer, real, or complex).


    This page titled 19.1: Character String to Numeric Conversion is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.