diff --git a/collects/racket/format.rkt b/collects/racket/format.rkt index ec04ba6106..d2e3d36bdf 100644 --- a/collects/racket/format.rkt +++ b/collects/racket/format.rkt @@ -303,6 +303,15 @@ [else (values np N*)]))))) (define (%exponential N-abs base upper? format-exponent significand-precision exactly?) + (cond [(zero? N-abs) + (string-append "0" + (if exactly? "." "") + (if exactly? (make-string significand-precision #\0) "") + (exponent-part 0 format-exponent base))] + [else + (%exponential-nz N-abs base upper? format-exponent significand-precision exactly?)])) + +(define (%exponential-nz N-abs base upper? format-exponent significand-precision exactly?) (define-values (N* e-adjust actual-precision) (scale N-abs base significand-precision exactly?)) ;; hack: from 1234 want "1.234"; convert to "1234", mutate to ".234" after saving "1" @@ -312,19 +321,22 @@ (string-set! digits 0 #\.) (string-append leading-digit (if (or exactly? (positive? actual-precision)) digits "") - (cond [(procedure? format-exponent) - (format-exponent exponent)] - [else - (string-append - (cond [(string? format-exponent) format-exponent] - [(= base 10) "e"] - [else (format "*~s^" base)]) - (if (negative? exponent) "-" "+") - (%pad (number->string (abs exponent)) - #:pad-to 2 - #:align 'right - #:left-padding "0" - #:right-padding #f))])))) + (exponent-part exponent format-exponent base)))) + +(define (exponent-part exponent format-exponent base) + (cond [(procedure? format-exponent) + (format-exponent exponent)] + [else + (string-append + (cond [(string? format-exponent) format-exponent] + [(= base 10) "e"] + [else (format "*~s^" base)]) + (if (negative? exponent) "-" "+") + (%pad (number->string (abs exponent)) + #:pad-to 2 + #:align 'right + #:left-padding "0" + #:right-padding #f))])) (define (scale N-abs base significand-precision exactly?) (if (zero? N-abs) diff --git a/collects/tests/racket/format.rkt b/collects/tests/racket/format.rkt index 195e610a07..62d4a35c2f 100644 --- a/collects/tests/racket/format.rkt +++ b/collects/tests/racket/format.rkt @@ -231,6 +231,13 @@ (tc (~r #:notation 'positional 3735928559 #:base '(up 16)) "DEADBEEF") +(tc (~r #:notation 'positional 0) + "0") +(tc (~r #:notation 'positional 0 #:precision 4) + "0") +(tc (~r #:notation 'positional 0 #:precision '(= 4)) + "0.0000") + ;; ~r #:notation 'exponential (tc (~r 12345 #:precision 3 #:notation 'exponential) @@ -295,3 +302,10 @@ "-34.00") (tc (~r -33.99508664763296 #:precision '(= 3)) "-33.995") + +(tc (~r #:notation 'exponential 0) + "0e+00") +(tc (~r #:notation 'exponential 0 #:precision 4) + "0e+00") +(tc (~r #:notation 'exponential 0 #:precision '(= 4)) + "0.0000e+00")