Skip to content

Commit

Permalink
cl-format: ~$ wasn't handling very small fractions (that round to 0) …
Browse files Browse the repository at this point in the history
…correctly.

refs #40
  • Loading branch information
tomfaulhaber committed Oct 31, 2009
1 parent 3f26704 commit 6f9e9c2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
28 changes: 15 additions & 13 deletions src/clojure/contrib/pprint/cl-format.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand Down
8 changes: 7 additions & 1 deletion src/clojure/contrib/test_contrib/pprint/cl_format.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 6f9e9c2

Please sign in to comment.