assemble_fixed Function

private pure function assemble_fixed(negative, rounded, n_rounded, point_pos, dec) result(string)

Place the decimal point in an already rounded digit string.

The invariant maintained by round_decimal is that n_rounded equals point_pos + dec whenever any digit survives, so splitting at point_pos always leaves exactly dec fractional digits.

Arguments

Type IntentOptional Attributes Name
logical, intent(in) :: negative
character(len=*), intent(in) :: rounded
integer(kind=default_int), intent(in) :: n_rounded
integer(kind=default_int), intent(in) :: point_pos
integer(kind=default_int), intent(in) :: dec

Return Value character(len=:), allocatable


Called by

proc~~assemble_fixed~~CalledByGraph proc~assemble_fixed assemble_fixed proc~to_string_fixed_dp to_string_fixed_dp proc~to_string_fixed_dp->proc~assemble_fixed interface~to_string_fixed to_string_fixed interface~to_string_fixed->proc~to_string_fixed_dp proc~to_string_fixed_sp to_string_fixed_sp interface~to_string_fixed->proc~to_string_fixed_sp proc~to_string_fixed_sp->proc~to_string_fixed_dp

Variables

Type Visibility Attributes Name Initial
character(len=:), private, allocatable :: frac_part
character(len=:), private, allocatable :: int_part

Source Code

   pure function assemble_fixed(negative, rounded, n_rounded, point_pos, dec) result(string)
      !! Place the decimal point in an already rounded digit string.
      !!
      !! The invariant maintained by `round_decimal` is that `n_rounded` equals
      !! `point_pos + dec` whenever any digit survives, so splitting at
      !! `point_pos` always leaves exactly `dec` fractional digits.
      logical, intent(in) :: negative
      character(len=*), intent(in) :: rounded
      integer(default_int), intent(in) :: n_rounded, point_pos, dec
      character(len=:), allocatable :: string
      character(len=:), allocatable :: int_part, frac_part

      if (n_rounded <= 0_default_int) then
         int_part = "0"
         frac_part = repeat("0", dec)
      else if (point_pos <= 0_default_int) then
         int_part = "0"
         frac_part = repeat("0", -point_pos)//rounded(1:n_rounded)
      else
         int_part = rounded(1:point_pos)
         frac_part = rounded(point_pos + 1:n_rounded)
      end if

      if (dec > 0_default_int) then
         string = int_part//"."//frac_part
      else
         string = int_part
      end if
      if (negative) string = "-"//string

   end function assemble_fixed