Rounding numbers to n decimals in clojure
When I do, for example, (/ 1.0 7.0), I only get 17 di开发者_C百科gits: 0.14285714285714285. How can I get more then that?
Use BigDecimal
numbers and with-precision
:
(with-precision 50 (/ 1M 7))
=> 0.14285714285714285714285714285714285714285714285714M
Clojure (and pretty much all lisps) has ratio type that way you do not lose precision. do all your calculations with ratio and convert to double/float at the last minute to avoid precision problems.
精彩评论