diff options
author | Tom Faulhaber <git_net@infolace.com> | 2009-10-31 00:38:29 -0700 |
---|---|---|
committer | Tom Faulhaber <git_net@infolace.com> | 2009-10-31 00:38:29 -0700 |
commit | 6f9e9c276bf53b0fce2275abf29a25f9735a6409 (patch) | |
tree | 45a789b66d99164ac49fdd7c2e694219bbcc3b3c | |
parent | 3f267049a38f205ac3c7d91173c63648f139e790 (diff) |
cl-format: ~$ wasn't handling very small fractions (that round to 0) correctly.
refs #40
-rw-r--r-- | src/clojure/contrib/pprint/cl-format.clj | 28 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/pprint/cl_format.clj | 8 |
2 files changed, 22 insertions, 14 deletions
diff --git a/src/clojure/contrib/pprint/cl-format.clj b/src/clojure/contrib/pprint/cl-format.clj index b0531c14..6fd691e2 100644 --- a/src/clojure/contrib/pprint/cl-format.clj +++ b/src/clojure/contrib/pprint/cl-format.clj @@ -575,19 +575,21 @@ Note this should only be used for the last one in the sequence" [(str "0" m) (inc e) 1 (inc len)] [m e round-pos len])] (if round-pos - (if (> len round-pos) - (let [round-char (nth m1 round-pos) - #^String result (subs m1 0 round-pos)] - (if (>= (int round-char) (int \5)) - (let [result-val (Integer/valueOf result) - leading-zeros (subs result 0 (min (prefix-count result \0) (- round-pos 1))) - round-up-result (str leading-zeros - (String/valueOf (+ result-val - (if (neg? result-val) -1 1)))) - expanded (> (count round-up-result) (count result))] - [round-up-result e1 expanded]) - [result e1 false])) - [m e false]) + (if (neg? round-pos) + ["0" 0 false] + (if (> len round-pos) + (let [round-char (nth m1 round-pos) + #^String result (subs m1 0 round-pos)] + (if (>= (int round-char) (int \5)) + (let [result-val (Integer/valueOf result) + leading-zeros (subs result 0 (min (prefix-count result \0) (- round-pos 1))) + round-up-result (str leading-zeros + (String/valueOf (+ result-val + (if (neg? result-val) -1 1)))) + expanded (> (count round-up-result) (count result))] + [round-up-result e1 expanded]) + [result e1 false])) + [m e false])) [m e false])) [m e false])) diff --git a/src/clojure/contrib/test_contrib/pprint/cl_format.clj b/src/clojure/contrib/test_contrib/pprint/cl_format.clj index 6684bff2..6c73e63f 100644 --- a/src/clojure/contrib/test_contrib/pprint/cl_format.clj +++ b/src/clojure/contrib/test_contrib/pprint/cl_format.clj @@ -188,7 +188,13 @@ (cl-format nil "~1,1,8,' @:$" -12.0) "- 12.0" (cl-format nil "~1,1,8,' @$" -12.0) " -12.0" (cl-format nil "~1,1,8,' :$" -12.0) "- 12.0" - (cl-format nil "~1,1,8,' $" -12.0) " -12.0") + (cl-format nil "~1,1,8,' $" -12.0) " -12.0" + (cl-format nil "~1,1$" 0.001) "0.0" + (cl-format nil "~2,1$" 0.001) "0.00" + (cl-format nil "~1,1,6$" 0.001) " 0.0" + (cl-format nil "~1,1,6$" 0.0015) " 0.0" + (cl-format nil "~2,1,6$" 0.005) " 0.01" + (cl-format nil "~2,1,6$" 0.01) " 0.01") (simple-tests f-tests (cl-format nil "~,1f" -12.0) "-12.0") |