开发者

Rounding numbers to n decimals in clojure

开发者 https://www.devze.com 2023-03-28 20:26 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消