Lisp HUG Maillist Archive

Engineering Format

oh well... this routine works passably well enough...

(defun float-to-engineering-format-string (val)
  (if (or (zerop val)
          (and (<= 0.01 (abs val))
               (< (abs val) 10000)))
    ;; for values between 0.01 and 9,999 just use F format
    (format nil "~,3F" (float val 1.0))
    
    ;; Engineering notation
    (let* ((pwr (* 3 (floor (log (abs val) 10) 3)))
           (v   (/ val (expt 10 pwr))))
      (when (>= (round (abs v)) 1000)
        (incf pwr 3)
        (setf v (* 0.001 v)))
      (format nil "~,2Fe~d" v pwr)
      )))

David McClain
Chief Technical Officer
Refined Audiometrics Laboratory
4391 N. Camino Ferreo
Tucson, AZ  85750

email: dbm@refined-audiometrics.com
phone: 1.520.390.3995
web: http://www.refined-audiometrics.com


Updated at: 2020-12-10 08:44 UTC