This doesn't seem to work for non-terminating decimals like 1/3:
(defn round [s n]
> (.setScale (bigdec n) s java.math.RoundingMode/HALF_EVEN))
> #'user/round
> user=> (round 3 (/ 1 3))
> ArithmeticException Non-terminating decimal expansion; no exact
> representable decimal result. java.mat
I think that is what Brenton posted on Mar 23.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your
first post.
To u
Thanks so much for your answers.
OK. So from the solutions above I understand that Clojure does not
have C#'s equivalent of Math.round(7.127298, 2), and requires either
using some custom method or Java's rounding?
I think I'll rather go with the Java-interop approach.
On Mar 23, 11:13 pm, Joost
On Mar 23, 8:07 pm, Meikel Brandmeyer wrote:
> Hi,
>
> a bit naive, but it seems to work…
>
> user=> (defn round
> [x & {p :precision}]
> (if p
> (let [scale (Math/pow 10 p)]
> (-> x (* scale) Math/round (/ scale)))
> (Math/round x)))
> #'user/r
You may also consider using java interop here.
(defn round [s n]
(.setScale (bigdec n) s java.math.RoundingMode/HALF_EVEN))
=> (round 3 78.37898794)
=> 78.379
see
http://download.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html
Brenton
On Mar 23, 11:08 am, JDuPreez wrote:
> I'm t
Another way:
(defn myround [x precision] (-> x (bigdec) (.movePointRight precision) (+
0.5) (int) (bigdec) (.movePointLeft precision)))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Not
Hi,
a bit naive, but it seems to work…
user=> (defn round
[x & {p :precision}]
(if p
(let [scale (Math/pow 10 p)]
(-> x (* scale) Math/round (/ scale)))
(Math/round x)))
#'user/round
user=> (round 78.37898794)
78
user=> (round 78.37898794 :prec