home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!apple!apple!cambridge.apple.com!wilcox@cmns.think.com
- From: wilcox@cmns.think.com
- Newsgroups: comp.lang.lisp.mcl
- Subject: _numtostring
- Message-ID: <9208142145.AA06224@cmns-sun.think.com>
- Date: 14 Aug 92 21:45:10 GMT
- References: Daniel Berger's message of Thu, 13 Aug 92 11:32:58 PDT <9208131832.AA01924@dewey>
- Sender: info-mcl-request@cambridge.apple.com
- Lines: 33
- Approved: comp.lang.lisp.mcl@Cambridge.Apple.C0M
- Original-To: berger@soe.berkeley.edu
- Original-Cc: info-mcl@cambridge.apple.com
-
- 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)
- )))))
-