Skip to content
Snippets Groups Projects
Commit 9ae7c238 authored by Allen Gilliland's avatar Allen Gilliland
Browse files

tweak our number formatting function to be a bit more forgiving and treat...

tweak our number formatting function to be a bit more forgiving and treat decimals with no actual decimal values as if they are whole numbers.  also adding in some unit tests.
parent e58dd659
No related branches found
No related tags found
No related merge requests found
......@@ -119,12 +119,14 @@
"format a number into a more human readable form."
[number]
{:pre [(number? number)]}
(if (or (float? number)
(decimal? number))
;; looks like this is a decimal number, format with precision of 2
(format "%,.2f" number)
;; otherwise expect this is a whole number
(format "%,d" number)))
(let [decimal-type? #(or (float? %) (decimal? %))]
(cond
;; looks like this is a decimal number, format with precision of 2
(and (decimal-type? number) (not= 0 (mod number 1))) (format "%,.2f" number)
;; this is a decimal type number with no actual decimal value, so treat it as a whole number
(decimal-type? number) (format "%,d" (long number))
;; otherwise this is a whole number
:else (format "%,d" number))))
(defn jdbc-clob->str
"Convert a `JdbcClob` or `PGobject` to a `String`."
......
......@@ -46,6 +46,26 @@
(host-port-up? "nosuchhost" 8005))
;; ## tests for `(format-num)`
(expect "1" (format-num 1))
(expect "1" (format-num (float 1)))
(expect "1" (format-num (double 1)))
(expect "1" (format-num (bigdec 1)))
(expect "1" (format-num (long 1)))
(expect "1.23" (format-num (float 1.23444)))
(expect "1.23" (format-num (double 1.23444)))
(expect "1.23" (format-num (bigdec 1.23444)))
(expect "1,234" (format-num 1234))
(expect "1,234" (format-num (float 1234)))
(expect "1,234" (format-num (double 1234)))
(expect "1,234" (format-num (bigdec 1234)))
(expect "1,234" (format-num (long 1234)))
(expect "1,234.56" (format-num (float 1234.5678)))
(expect "1,234.56" (format-num (double 1234.5678)))
(expect "1,234.56" (format-num (bigdec 1234.5678)))
;;; ## tests for IS-URL?
(expect true (is-url? "http://google.com"))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment