[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
_numtostring
- To: berger@soe.berkeley.edu
- Subject: _numtostring
- From: wilcox@cmns.think.com
- Date: Fri, 14 Aug 92 17:45:10 EDT
- Cc: info-mcl@cambridge.apple.com
- In-reply-to: Daniel Berger's message of Thu, 13 Aug 92 11:32:58 PDT <9208131832.AA01924@dewey>
I found a couple of bugs in the function float-to-string that I sent out
earlier. Specifically, it did not work well for negative numbers or numbers
which were slightly less than integers. Here's a revised version:
;;; this function needs a better name
(defun round-significand (number fractional-digits)
"Round the significand for display."
(if fractional-digits
(let ((divisor (expt 10 (- fractional-digits))))
(* (round number divisor) divisor))
(values number)
))
;;; this is a fast alternative to format for floating-point numbers
;;; with no exponent
(defun float-to-string (number fractional-digits)
"Converts a floating-point number to a string, ~
with a given number of digits following the decimal point."
(multiple-value-bind (integral fractional)
(truncate (round-significand number fractional-digits))
(let ((int-string (integer-to-string integral)))
;; dont mess around with fractions if you dont have to
(if (zerop fractional-digits)
(concatenate 'string int-string ".")
(let* ((fract-int (* (expt 10 fractional-digits)
;; use abs because truncate signs the remainder
(abs fractional)))
(fract-string (integer-to-string fract-int))
;; pad fraction with enough zeros to separate it from decimal
(pad-string (make-string (- fractional-digits (length fract-string))
:initial-element #\0)))
(concatenate 'string int-string "." pad-string fract-string)
)))))