      subroutine chrxf4 (real4, ch, ndp)

c Transform a real number REAL4 into a character string CH
c   with NDP decimal places - a decimal point will always be shown.

c Leading zeros will be replaced by leading blanks except for
c   the units column immediately before the decimal point.

c The sign will only be shown if the value is negative.

c If the character string width is only one more than the number
c   of decimal places then the leading zero is not given
c   (no room for it).

      character*(*) ch, msg*132
      real*8 biggest/2147483647.d0/
      real*8 value, real8, abreal, raised
      logical leadz

      value = real4
1     leadz = .false.
3     if (ndp .lt. 0) go to 90
      lch = len(ch)
      if (lch .le. ndp) go to 90
      abreal = abs(value)
      if (abreal .gt. biggest) go to 91
      nspace = lch - (ndp+1)      ! number of positions before dec pt
      if (nspace .lt. 10) then
        if (abreal .ge. 10.d0**nspace) then
          call cfill (ch, '*')
          return
        end if
      end if
      raised = abreal*10.d0**ndp
      if (raised .gt. biggest) go to 92
      num = idnint(raised)
      call chvxi0 (num, ch(2:), 10)
      if (2 .le. lch-ndp) then
        ch(1:lch-ndp) = ch(2:lch-ndp)//'.'
      else
        ch(1:1) = '.'
      end if
      i = 1
      if (.not.leadz) then
        do while (i .lt. nspace .and. ch(i:i) .eq. '0')
          i = i + 1
        end do
        i = i - 1
        if (i .gt. 0) ch(:i) = ' '
      end if
      if (value .le. 0.d0 .and. num .ne. 0) then
        if (i .gt. 0) then
          ch(i:i) = '-'
        else
          call cfill (ch, '*')
        end if
      end if
      return

90    write (msg, '(a,g22.7,a,i5,a,i5,a)')
     2     'CHRXF4 problem: Unable to show', value,
     2     ' in', lch, ' columns to ', ndp, ' decimal places'
      go to 99

91    write (msg, '(a,g22.7,a,g22.7,a)')
     2     'CHRXF4 problem: Values are limited to ', biggest,
     2     '. ', value, ' exceeds that limit'
      go to 99

92    write (msg, '(a,g22.7,a,i4,a)')
     2     'CHRXF4 problem: A value as large as', value,
     2     ' cannot be shown to ', ndp, ' decimal places'
99    call abort (msg(:len_trim(msg)))
      stop

      entry chrxf8 (real8, ch, ndp)       ! real*8 entry point
c     -----------------------------

      value = real8
      go to 1

      entry chrxf08 (real8, ch, ndp)      ! real*8 entry point
c     ------------------------------

      value = real8
      go to 2

      entry chrxf04 (real4, ch, ndp)      ! real*4 entry point
c     ------------------------------

      value = real4
2     leadz = .true.
      go to 3
      end
