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.math.BigDecimal.divide 
> (BigDecimal.java:1603)


 Is there a way of sending the scale to the bigdec function instead of 
setting it afterwards? It doesn't seem so from the docs..

// Pascal. 

On Wednesday, 23 March 2011 20:59:18 UTC+1, Brenton wrote:
>
> 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 <jacques...@gmail.com> wrote: 
> > I'm trying to round a decimal number like 78.37898794 to say 78.379, 
> > without converting it to a string. I've been struggling to get this 
> > right and to get info on it. The closest that I've found is to use: 
> > format "%.3f". But format converts the number to a string. Not exactly 
> > what I'm trying to do. 
> > 
> > How can I do this in Clojure, or where is info on how to do it? Thanks 
> > for your help.

-- 
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 unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to