Re: Noob Question - Clojure number rounding

2012-09-11 Thread Pascal Chatterjee
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

Re: Noob Question - Clojure number rounding

2011-03-26 Thread .Bill Smith
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

Re: Noob Question - Clojure number rounding

2011-03-25 Thread JDuPreez
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

Re: Noob Question - Clojure number rounding

2011-03-23 Thread 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

Re: Noob Question - Clojure number rounding

2011-03-23 Thread Brenton
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

Re: Noob Question - Clojure number rounding

2011-03-23 Thread .Bill Smith
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

Re: Noob Question - Clojure number rounding

2011-03-23 Thread Meikel Brandmeyer
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