Converting integer to string.
use penf
print "(A)", str(n=-1_I4P)
use penf
print "(A)", str(n=-1_I4P, no_sign=.true.)
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=I4P), | intent(in) | :: | n | Integer to be converted. |
||
logical, | intent(in), | optional | :: | no_sign | Flag for leaving out the sign. |
Returned string containing input number plus padding zeros.
elemental function str_I4P(n, no_sign) result(str)
!< Converting integer to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1_I4P)
!<```
!=> -1 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1_I4P, no_sign=.true.)
!<```
!=> 1 <<<
integer(I4P), intent(in) :: n !< Integer to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
character(DI4P) :: str !< Returned string containing input number plus padding zeros.
write(str, FI4P) n ! Casting of n to string.
str = adjustl(trim(str)) ! Removing white spaces.
if (n>=0_I4P) str='+'//trim(str) ! Prefixing plus if n>0.
if (present(no_sign)) str=str(2:) ! Leaving out the sign.
endfunction str_I4P