real_to_string Subroutine

public subroutine real_to_string(rval, real_fmt, compact_real, non_normals_to_null, str)

Convert a real value to a string.

Modified

  • Izaak Beekman : 02/24/2015 : added the compact option.
  • Jacob Williams : 10/27/2015 : added the star option.
  • Jacob Williams : 07/07/2019 : added null and ieee options.

Arguments

Type IntentOptional AttributesName
real(kind=RK), intent(in) :: rval

real value.

character(kind=CDK,len=*), intent(in) :: real_fmt

format for real numbers

logical(kind=LK), intent(in) :: compact_real

compact the string so that it is displayed with fewer characters

logical(kind=LK), intent(in) :: non_normals_to_null

If True, NaN, Infinity, or -Infinity are returned as null. If False, the string value will be returned in quotes (e.g., “NaN”, “Infinity”, or “-Infinity” )

character(kind=CK,len=*), intent(out) :: str

rval converted to a string.


Called by

proc~~real_to_string~~CalledByGraph proc~real_to_string real_to_string proc~json_get_string json_get_string proc~json_get_string->proc~real_to_string proc~json_value_print json_value_print proc~json_value_print->proc~real_to_string

Contents

Source Code


Source Code

    subroutine real_to_string(rval,real_fmt,compact_real,non_normals_to_null,str)

    implicit none

    real(RK),intent(in)                  :: rval         !! real value.
    character(kind=CDK,len=*),intent(in) :: real_fmt     !! format for real numbers
    logical(LK),intent(in)               :: compact_real !! compact the string so that it is
                                                         !! displayed with fewer characters
    logical(LK),intent(in)               :: non_normals_to_null !! If True, NaN, Infinity, or -Infinity are returned as `null`.
                                                                !! If False, the string value will be returned in quotes
                                                                !! (e.g., "NaN", "Infinity", or "-Infinity" )
    character(kind=CK,len=*),intent(out) :: str          !! `rval` converted to a string.

    integer(IK) :: istat !! write `iostat` flag

    if (ieee_is_finite(rval) .and. .not. ieee_is_nan(rval)) then

        ! normal real numbers

        if (real_fmt==star) then
            write(str,fmt=*,iostat=istat) rval
        else
            write(str,fmt=real_fmt,iostat=istat) rval
        end if

        if (istat==0) then
            !in this case, the default string will be compacted,
            ! so that the same value is displayed with fewer characters.
            if (compact_real) call compact_real_string(str)
        else
            str = repeat(star,len(str)) ! error
        end if

    else
        ! special cases for NaN, Infinity, and -Infinity

        if (non_normals_to_null) then
            ! return it as a JSON null value
            str = null_str
        else
            ! Let the compiler do the real to string conversion
            ! like before, but put the result in quotes so it
            ! gets printed as a string
            write(str,fmt=*,iostat=istat) rval
            if (istat==0) then
                str = quotation_mark//trim(adjustl(str))//quotation_mark
            else
                str = repeat(star,len(str)) ! error
            end if
        end if

    end if

    end subroutine real_to_string