diff options
Diffstat (limited to 'src/boot.clj')
-rw-r--r-- | src/boot.clj | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/src/boot.clj b/src/boot.clj index c9afa590..15323ab9 100644 --- a/src/boot.clj +++ b/src/boot.clj @@ -2284,4 +2284,20 @@ not-every? (comp not every?)) once, but any effects on Refs will be atomic." [& exprs] `(sync nil ~@exprs)) -
\ No newline at end of file + +(defmacro with-precision + "Sets the precision and rounding mode to be used for BigDecimal operations. + Usage: (with-precision 10 (/ 1M 3)) + or: (with-precision 10 :rounding HALF_DOWN (/ 1M 3)) + The rounding mode is one of CEILING, FLOOR, HALF_UP, HALF_DOWN, HALF_EVEN, + UP, DOWN and UNNECESSARY; it defaults to HALF_UP." + [precision & exprs] + (let [[body mc] + (if (= (first exprs) :rounding) + [(rest (rest exprs)) + (java.math.MathContext. precision + (. clojure.lang.Reflector getStaticField java.math.RoundingMode (name (second exprs))))] + [exprs + (java.math.MathContext. precision)])] + `(binding [*math-context* ~mc] + ~@body)))
\ No newline at end of file |