change real->decimal-string (PR 9285)
svn: r9210
This commit is contained in:
parent
021d4d7527
commit
cb3fdd8fc5
|
@ -15,18 +15,21 @@
|
|||
(define (real->decimal-string n [digits 2])
|
||||
(unless (exact-nonnegative-integer? digits)
|
||||
(raise-type-error 'real->decimal-string "exact-nonnegative-integer" n))
|
||||
(if (zero? digits)
|
||||
(number->string (inexact->exact (round n)))
|
||||
(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)))))))
|
||||
(let* ([e (expt 10 digits)]
|
||||
[num (round (abs (* e (inexact->exact n))))])
|
||||
(format "~a~a.~a"
|
||||
(if (or (negative? n)
|
||||
(equal? n -0.0))
|
||||
"-"
|
||||
"")
|
||||
(quotient num e)
|
||||
(if (zero? digits)
|
||||
""
|
||||
(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
|
||||
|
|
|
@ -5,20 +5,21 @@
|
|||
|
||||
;; to add when this library is there: (require scheme/string)
|
||||
|
||||
(test "0" real->decimal-string 0 0)
|
||||
(test "0" real->decimal-string 0.0 0)
|
||||
(test "1" real->decimal-string 0.6 0)
|
||||
(test "1" real->decimal-string 3/4 0)
|
||||
(test "1" real->decimal-string 1.2 0)
|
||||
(test "0" real->decimal-string -0.0 0) ; note this!
|
||||
(test "0.00" real->decimal-string -0.0) ; same here...
|
||||
(test "-1" real->decimal-string -0.6 0)
|
||||
(test "-1" real->decimal-string -3/4 0)
|
||||
(test "-1" real->decimal-string -1.2 0)
|
||||
(test "0." real->decimal-string 0 0)
|
||||
(test "0." real->decimal-string 0.0 0)
|
||||
(test "1." real->decimal-string 0.6 0)
|
||||
(test "1." real->decimal-string 3/4 0)
|
||||
(test "1." real->decimal-string 1.2 0)
|
||||
(test "-0." real->decimal-string -0.0 0) ; note this!
|
||||
(test "-0.00" real->decimal-string -0.0) ; same here...
|
||||
(test "-1." real->decimal-string -0.6 0)
|
||||
(test "-1." real->decimal-string -3/4 0)
|
||||
(test "-1." real->decimal-string -1.2 0)
|
||||
(test "1.20" real->decimal-string 1.2)
|
||||
(test "-1.20" real->decimal-string -1.2)
|
||||
(test "1.00" real->decimal-string 0.99999999999)
|
||||
(test "-1.00" real->decimal-string -0.99999999999)
|
||||
(test "1999999999999999859514578049071102439861518336.00" real->decimal-string 2e45)
|
||||
|
||||
(let ([s (list->string
|
||||
(let loop ([i 0])
|
||||
|
|
Loading…
Reference in New Issue
Block a user