Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by
recategorized by


I hope the core.typed is proper category, there is no "core" to select

Clojure 1.10.1

(with-precision 4 1.23456) ;; 1.23456
(with-precision 4 1.23456M) ;; 1.23456M
(with-precision 4 (BigDecimal. 1.23456)) ;; 1.2345600000000001017497197608463466167449951171875M
(with-precision 4 (* 1 (BigDecimal. 1.23456))) ;; 1.235M
(with-precision 4 (+ 0 (BigDecimal. 1.23456))) ;; 1.235M

it only seems to work as expected when the number is supplied as result of * or +


There's a top-level Clojure category -- core.typed is a specific Contrib library (although I'm not sure which subcategory of Clojure it belongs in... Alex?
You don't need to set a subcategory, top level Clojure is fine.

1 Answer

0 votes

That is correct, per the docstring, "Sets the precision and rounding mode to be used for BigDecimal operations".

Thanks for pointing it out. What is rationale for this?
It is in the doc but ergonomicaly this looks like special case
I think you misunderstand the purpose of this macro - it sets the precision and rounding mode to use while performing math using BigDecimals. If you are looking to change the precision of a single value, there are Java APIs on the BigDecimal class that does that. See https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html for more details. If you are looking to change the scale, you can do that for example with (.setScale 1.23456M 4 java.math.RoundingMode/DOWN).