From 0ee9f22efd56d7a192ae5529c0d0690ca69cebb4 Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Thu, 26 Oct 2006 06:55:29 +0000 Subject: [PATCH] added real->decimal-string (one piece of a real formatter) svn: r4685 original commit: 58191912c6f0e292e60cb7d537c2508fe5bae71f --- collects/mzlib/string.ss | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/collects/mzlib/string.ss b/collects/mzlib/string.ss index 9195545..17b5745 100644 --- a/collects/mzlib/string.ss +++ b/collects/mzlib/string.ss @@ -6,6 +6,7 @@ read-from-string read-from-string-all expr->string + real->decimal-string regexp-quote regexp-replace-quote regexp-match* @@ -108,6 +109,21 @@ (write v port) (get-output-string port)))) + ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + (define real->decimal-string + (opt-lambda (n [digits 2]) + (let* ([e (expt 10 digits)] + [num (round (abs (* e (inexact->exact n))))]) + (format "~a~a.~a" + (if (negative? n) "-" "") + (quotient num e) + (let ([s (number->string (remainder num e))]) + (if (= (string-length s) digits) + s + (string-append (make-string (- digits (string-length s)) #\0) + s))))))) + ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Regexp helpers