fmt_number Function

private pure function fmt_number(digits, p) result(s)

Format digits x 10^p as giza_format_number writes it in automatic form.

Handles mantissas beyond giza's C int.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: digits

Decimal digits of the mantissa.

integer(kind=int32), intent(in) :: p

Power of ten.

Return Value character(len=64)


Source Code

   pure function fmt_number(digits, p) result(s)
      !! Format `digits x 10^p` as `giza_format_number` writes it in automatic form.
      !!
      !! Handles mantissas beyond giza's C `int`.
      ! Arguments
      character(len=*), intent(in) :: digits
         !! Decimal digits of the mantissa.
      integer(int32), intent(in) :: p
         !! Power of ten.
      character(len=64) :: s
      character(len=32) :: d, ex
      integer(int32) :: nd, pw, dbp

      d = digits; pw = p
      nd = len_trim(d)
      do while (nd > 1 .and. d(nd:nd) == '0')
         d(nd:nd) = ' '
         nd = nd - 1
         pw = pw + 1
      end do
      dbp = nd + min(pw, 0)
      if (pw >= 0 .and. pw + nd <= 4) then
         s = trim(d)//repeat('0', pw)
         return
      else if (dbp >= 1 .and. dbp <= 4 .and. dbp < nd) then
         s = d(1:dbp)//'.'//d(dbp + 1:nd)
         return
      end if
      pw = pw + nd - 1
      if (pw == -1 .or. pw == -2) then
         d = repeat('0', -pw)//d
         nd = nd - pw
         pw = 0
      end if
      s = d(1:1)
      if (nd > 1) s = d(1:1)//'.'//d(2:nd)
      if (pw /= 0) then
         write (ex, '(i0)') pw
         s = trim(s)//'\times10^{'//trim(ex)//'}'
      end if
   end function fmt_number