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

Merge pull request #340 from metabase/ag-more-num-formatting

more number formatting for email reports
parents e58dd659 37e8467e
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 (zero? (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,36 @@
(host-port-up? "nosuchhost" 8005))
;; ## tests for `(format-num)`
;; basic whole number case
(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)))
;; make sure we correctly format down to 2 decimal places
;; note that we are expecting a round DOWN in this case
(expect "1.23" (format-num (float 1.23444)))
(expect "1.23" (format-num (double 1.23444)))
(expect "1.23" (format-num (bigdec 1.23444)))
;; test that we always force precision of 2 on decimal places
(expect "1.20" (format-num (float 1.2)))
(expect "1.20" (format-num (double 1.2)))
(expect "1.20" (format-num (bigdec 1.2)))
;; we can take big numbers and add in commas
(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)))
;; we can handle numbers with both commas and decimal places
;; note that we expect a basic round UP here
(expect "1,234.57" (format-num (float 1234.5678)))
(expect "1,234.57" (format-num (double 1234.5678)))
(expect "1,234.57" (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