@@ -538,3 +538,72 @@ program demo_count
538538
539539end program demo_count
540540```
541+
542+ <!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
543+ ### ` to_string `
544+
545+ #### Description
546+
547+ Format or transfer a ` integer/real/complex/logical ` scalar as a string.
548+ Input a wrong ` format ` that cause the internal-IO to fail, the result value is a string of ` [*] ` .
549+
550+ #### Syntax
551+
552+ ` string = [[stdlib_strings(module):to_string(interface)]] (value [, format]) `
553+
554+ #### Status
555+
556+ Experimental
557+
558+ #### Class
559+
560+ Pure function.
561+
562+ #### Argument
563+
564+ - ` value ` : Shall be an ` integer/real/complex/logical ` scalar.
565+ This is an ` intent(in) ` argument.
566+ - ` format ` : Shall be a ` character(len=*) ` scalar like ` '(F6.2)' ` or just ` 'F6.2' ` .
567+ This is an ` intent(in) ` and ` optional ` argument.
568+ Contains the edit descriptor to format ` value ` into a string, for example ` '(F6.2)' ` or ` '(f6.2)' ` .
569+ ` to_string ` will automatically enclose ` format ` in a set of parentheses, so passing ` F6.2 ` or ` f6.2 ` as ` format ` is possible as well.
570+
571+ #### Result value
572+
573+ The result is an ` allocatable ` length ` character ` scalar with up to ` 128 ` cached ` character ` length.
574+
575+ #### Example
576+
577+ ``` fortran
578+ program demo_to_string
579+ use stdlib_strings, only: to_string
580+
581+ !> Example for `complex` type
582+ print *, to_string((1, 1)) !! "(1.00000000,1.00000000)"
583+ print *, to_string((1, 1), '(F6.2)') !! "( 1.00, 1.00)"
584+ print *, to_string((1000, 1), '(ES0.2)'), to_string((1000, 1), '(SP,F6.3)')
585+ !! "(1.00E+3,1.00)""(******,+1.000)"
586+ !! Too narrow formatter for real number
587+ !! Normal demonstration(`******` from Fortran Standard)
588+
589+ !> Example for `integer` type
590+ print *, to_string(-3) !! "-3"
591+ print *, to_string(42, '(I4)') !! " 42"
592+ print *, to_string(1, '(I0.4)'), to_string(2, '(B4)') !! "0001"" 10"
593+
594+ !> Example for `real` type
595+ print *, to_string(1.) !! "1.00000000"
596+ print *, to_string(1., '(F6.2)') !! " 1.00"
597+ print *, to_string(1., 'F6.2') !! " 1.00"
598+ print *, to_string(1., '(SP,ES9.2)'), to_string(1, '(F7.3)') !! "+1.00E+00""[*]"
599+ !! 1 wrong demonstration (`[*]` from `to_string`)
600+
601+ !> Example for `logical` type
602+ print *, to_string(.true.) !! "T"
603+ print *, to_string(.true., '(L2)') !! " T"
604+ print *, to_string(.true., 'L2') !! " T"
605+ print *, to_string(.false., '(I5)') !! "[*]"
606+ !! 1 wrong demonstrations(`[*]` from `to_string`)
607+
608+ end program demo_to_string
609+ ```
0 commit comments