开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜