;;AUTOLOAD: expand-real
;;AUTODOC: expand-real "converts a real into a string in a fixed place"
;; (expand-real number integral-places decimal-places 
;;              &optional omit-zeros decimal-point)
;;
;; Formats a real number into a printable representation and returns the 
;; printed string. pads before the integral part with spaces and after the
;; decimals with zeros. This function allows for formatting reals in collumns 
;; aligned on the decimal point.
;; If omit-zeros is non-nil, the trailing zeros are omitted, and even the whole
;; decimal part if all zeros.
;; decimal-point can be a string to specify the representation of the decimal 
;; point, defaults to "."
;;
;; Exemple:
;; (dolist (x '(1.0 12.1 2.3345)) (? (expand-real x 3 2 t) "\n"))
;;   1
;;  12.10
;;   2.33

(setq expand-real:re (regcomp "^([0-9+-]+)[.]([0-9]*)$"))
(setq expand-real:re-zeros (regcomp "^0*$"))
(defun expand-real (x before after 
    &optional omit-zeros (decimal-point ".")
    &aux s
    (*real-precision* 1000))		;avoid scientific notation
  (if (regexec expand-real:re (setq s (PF String "%0" (Real x)))) 
    (with (ip (regsub expand-real:re 1) fp (regsub expand-real:re 2))
      (+
	(make-string (- before (length ip)))
	ip
	(if (and omit-zeros (regexec expand-real:re-zeros fp))
	""
	(+ decimal-point (if (> (length fp) after) (subseq fp 0 after) fp)
	  (make-string (- after (length fp)) #\0)
    ))))
    s
))
