Converts a floating-point number to a character string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=dp), | intent(in) | :: | f |
input floating-point number |
||
character(len=*), | intent(in), | optional | :: | format_string |
Optional format string. |
Output character string.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
character(len=32), | private | :: | default_format | ||||
character(len=32), | private | :: | user_format |
function float_to_character(f, format_string) result(res)
!! Converts a floating-point number to a character string.
!---------------------------------------------------------------------!
real(dp), intent(in) :: f
!! input floating-point number
character(len=*), intent(in), optional :: format_string
!! Optional format string.
character(len=64) :: res
!! Output character string.
!---------------------------------------------------------------------!
character(len=32) :: default_format, user_format
!---------------------------------------------------------------------!
! Default format: 6 decimal places
!---------------------------------------------------------------------!
default_format = '(F0.6)'
!---------------------------------------------------------------------!
if (present(format_string)) then
user_format = trim(format_string)
else
user_format = default_format
endif
!---------------------------------------------------------------------!
write(res, user_format) f
res = adjustl(res)
!---------------------------------------------------------------------!
end function float_to_character