Fixed point rendering of a real(dp) value.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | value | |||
| integer(kind=default_int), | intent(in) | :: | decimals |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer(kind=default_int), | private | :: | bump | ||||
| integer(kind=default_int), | private | :: | dec | ||||
| character(len=WORK_LEN), | private | :: | exact | ||||
| integer(kind=default_int), | private | :: | n_exact | ||||
| integer(kind=default_int), | private | :: | n_keep | ||||
| integer(kind=default_int), | private | :: | n_rounded | ||||
| logical, | private | :: | negative | ||||
| integer(kind=default_int), | private | :: | point_pos | ||||
| character(len=WORK_LEN), | private | :: | rounded | ||||
| logical, | private | :: | special |
pure function to_string_fixed_dp(value, decimals) result(string) !! Fixed point rendering of a `real(dp)` value. real(dp), intent(in) :: value integer(default_int), intent(in) :: decimals character(len=:), allocatable :: string character(len=WORK_LEN) :: exact, rounded integer(default_int) :: n_exact, point_pos, dec, n_keep, n_rounded, bump logical :: negative, special call special_text(value, string, special) if (special) return dec = max(0_default_int, min(decimals, PIC_FORMAT_MAX_DECIMALS)) negative = is_negative(value) if (value == 0.0_dp) then ! Zero never carries a sign. Recognising a negative zero requires SIGN ! to distinguish the two signed zeros, which Fortran leaves processor ! dependent (F2018 16.9.165: the result is |A| if the processor cannot ! distinguish them), and Intel and AOCC do not distinguish them under ! their default fast floating point model. Emitting "-0.00" would ! therefore be compiler dependent, which is the one thing this module ! exists to avoid. A non-zero value that merely rounds to zero does ! keep its sign: SIGN is well defined for a non-zero argument. negative = .false. rounded = "" n_rounded = 0_default_int point_pos = 0_default_int else call expand_exact(abs(value), exact, n_exact, point_pos) n_keep = point_pos + dec call round_decimal(exact, n_exact, n_keep, rounded, n_rounded, bump) point_pos = point_pos + bump end if string = assemble_fixed(negative, rounded, n_rounded, point_pos, dec) end function to_string_fixed_dp