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