Manual Reference Pages  - fmt (3m_overload)

NAME

fmt(3f) - [M_overload] convert any intrinsic to a string using specified format (LICENSE:PD)

CONTENTS

Synopsis
Description
Options
Returns
Examples
Author
License

SYNOPSIS

function fmt(value,format) result(string)

    class(*),intent(in),optional :: value
    character(len=*),intent(in),optional :: format
    character(len=:),allocatable :: string

DESCRIPTION

FMT(3f) converts any standard intrinsic value to a string using the specified format.

OPTIONS

value value to print the value of. May be of type INTEGER, LOGICAL, REAL, DOUBLEPRECISION, COMPLEX, or CHARACTER.
format format to use to print value. It is up to the user to use an appropriate format. The format does not require being surrounded by parenthesis. If not present a default is selected similar to what would be produced with free format, with trailing zeros removed.

RETURNS

string A string value

EXAMPLES

Sample program:

<<<<<<< HEAD
    program demo_fmt
    use :: M_overload, only : fmt
    use :: M_overload, only : operator(.fmt.)
    use :: M_overload, only : operator( // )
    implicit none
    character(len=:),allocatable :: output
    character(len=:), allocatable :: string

write(*,*)’result is ’, fmt(10,"’[’,i0,’]’")

write(*,*)’result is ’, fmt(10.0/3.0,"’[’,g0.5,’]’")

output=fmt(.true.,"’The final answer is [’,g0,’]’") write(*,*)’result is ’,output ! operator string=1234.fmt.’"[",i0,"]"’ write(*,*)string ! tests write(*,*)’the value is ’,fmt(1234.5679,’f0.3’),’ or there-abouts’

write(*,*)1234.0 .fmt. ’"[",g0,"]"’ ! .eq. ’[1234]’

string=10//’ is an integer and ’//(11.00,22.00)//’ is a complex’ write(*,*)string

end program demo_fmt

Results:
> result is [10]
> result is [3.3333]
> result is The final answer is [T]
> [1234]
> the value is 1234.568 or there-abouts
> [1234.00000]
> 10 is an integer and <ERROR>End of file is a complex ================================================================================

======= program demo_fmt use :: M_overload, only : f=>fmt use :: M_overload, only : operator(.fmt.) use :: M_overload, only : operator( // ) implicit none character(len=:), allocatable :: output character(len=:), allocatable :: string ! some formats character(len=*), parameter :: bracket=’"[",g0.5,"]"’ character(len=*), parameter :: five=’g0.5’ character(len=*), parameter :: g0=’g0’ ! for statements character(len=*), parameter :: gen=’(*(g0:,1x))’ character(len=*), parameter :: asis=’(*(g0:))’ character(len=*), parameter :: comma=’(*(g0:,","))’ real :: x integer :: i real,allocatable :: arr(:)

! print will do, just want to control format of a number print gen,’result is’,10,’and’,f(10.0/3.0,’f0.3’) ! complex formats can be in nicely named strings, of course write(*,*)’result is ’, f(10.0/3.0,bracket) ! you can build a string without using an internal write output=f(.true.,"’The final answer is [’,g0,’]’") write(*,*)’the string is now:’,output x=1234.5680088 i=543 ! operator style string=1234.4567 .fmt.’g0.0’ write(*,*)string ! function style string=f(1234.4567, ’g0.0’) write(*,*)string ! concatenation style string=10//’ is an integer and ’//(11.00,22.00)//’ is a complex’ write(*,*)string ! list-directed I/O leaves column 1 blank and often prints more ! digits than warranted for the precision of a value. ! ! combined with a g0 format line "asis" you get something very ! similar to list-directed I/O accept no unexpected line breaks ! and starts in column 1 and lets you tweek float values and ! not have to remember a space goes between values unless they ! are adjacent strings, and you may or may not get while space ! after numeric values to make them all the same length for a ! particular type for nice tables or compact for use in composed text ! ! this is nearly as simple as list-directed but more predictable: print asis,’The value is ’,f(x,’f0.3’),’ or there-abouts’

! and combine multiple formats for use in a single line without ! using non-advancing I/O write(*,comma)x,f(x),f(x,g0),f(x,five),f(x,bracket)

! A common extension in Fortran is a VFE ! VFE: ! A Variable FORMAT Expression (VFE) is a format statement which ! includes angle brackets enclosing a Fortran expression where the ! value of the variable enclosed in the <> brackets is substituted ! as a string into the format. So if N=3 this format ! "FORMAT(I<N>)" would become "FORMAT(I3)". GNU Fortran does not ! support this legacy extension. The effect of variable format ! expressions can be reproduced by using the more powerful ! (and standard) combination of internal output and string formats. ! but it is easier using the overloaded // operator: VFE: block integer,allocatable :: vector(:) integer :: neg, zero, pos integer :: left vector=[-1,-22,-300,0,0,0,0,8,9,10,11,12] neg=3 zero=4 pos=5 write(*, "(1x,"//& &neg//"(’N’,i0:,1x),"//& &zero//"(’@’,i0:,1x),"//& &pos//"(’P’,i0:,1x))") vector endblock VFE ! !not yet! write(*,asis)’[’//f(arr,comma)//’]’

end program demo_fmt

Results:

      > result is 10 and 3.333
      >  result is [3.3333]
      >  the string is now:The final answer is [T]
      >  1234.45667
      >  1234.45667
      >  10 is an integer and (11,22) is a complex
      > The value is 1234.568 or there-abouts
      > 1234.56799,1234.56799,1234.56799,1234.6,[1234.6]
      >  N-1 N-22 N-300 @0 @0 @0 @0 P8 P9 P10 P11 P12

>>>>>>> 255ef6b (update)

AUTHOR

John S. Urban

LICENSE

Public Domain


<<<<<<< HEAD <<<<<<< HEAD
Nemo Release 3.1 fmt (3m_overload) July 21, 2024
Generated by manServer 1.08 from f131ebf6-4ec8-4942-81f4-c0cb0189944d using man macros. =======
Nemo Release 3.1 fmt (3m_overload) July 21, 2024
Generated by manServer 1.08 from dba363a0-94b2-48ec-b361-48689a2d635b using man macros. >>>>>>> 255ef6b (update) =======
Nemo Release 3.1 fmt (3m_overload) July 24, 2024
Generated by manServer 1.08 from 6b1c4416-93f4-44ab-b2fb-ce9190eea4c3 using man macros. >>>>>>> dbccb68 (cleanup)