91

When I do (/ 411 125) , I don't get it in terms of decimal. How do I do that?

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
unj2
  • 52,135
  • 87
  • 247
  • 375

6 Answers6

103
user> (float (/ 411 125))
3.288
user> (double (/ 411 125))
3.288
Brian Carper
  • 71,150
  • 28
  • 166
  • 168
37
user=> (clojure-version)
"1.4.0"

user=> (doc quot)
-------------------------
clojure.core/quot
([num div])
  quot[ient] of dividing numerator by denominator.
nil

user=> (quot 411 125)
3
Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
  • doc doesn't work for me. I have the same version of clojure. Do I need to import something? – justinhj Dec 02 '12 at 02:06
  • 1
    Sorry, I should've said it - it's a REPL session managed by [leiningen](http://leiningen.org). It's *the* project (and therefore dependency) management tool for Clojure projects. Once you get it going, you won't regret. – Jacek Laskowski Dec 18 '12 at 09:12
  • What version of lein do you use? I'm at lein 2 built from the sources. – Jacek Laskowski Dec 24 '12 at 23:24
  • Weird. According to [doc](http://clojure.github.com/clojure/clojure.repl-api.html#clojure.repl/doc) it was available in Clojure 1.0 so it should be in lein repl, too. What version of lein do you use exactly? What does lein print out when you run `doc`? – Jacek Laskowski Dec 26 '12 at 16:38
  • Ah, I found the problem. clojure.repl is not loaded when using slime ( I should have mentioned I am in emacs), because equivalent functions are provided differently. But can do (require 'clojure.repl) (clojure.repl/doc quot) – justinhj Dec 26 '12 at 17:01
  • 1
    I'm glad to hear it has finally worked fine. Congratulations! Keep an eye on questions about doc and Clojure when slime is used - you might sort it out very easily now. – Jacek Laskowski Dec 26 '12 at 19:49
15

As documented, integer division yields rational numbers. Try

(/ 411.0 125)
Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
7

If you use a float for the dividend, you'll get a decimal answer.

(/ 22.0 7) -> 3.142857142857143

There's also the (unchecked-remainder x y) function available.

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
6

even this will work:

(/ 22. 7) => 3.142857142857143
Samir
  • 183
  • 1
  • 6
  • 2
    You should also include some explanations on why your code works and what is wrong in the original code. – vyegorov Oct 26 '14 at 22:46
  • 1
    nothing wrong with other solutions, but - like other solutions - i'm representing another way of thinking in doubles. as for why it's working; i'm not sure but i think clojure inherited it from java which accept any digit with decimal point as Double even if it's formatted with zero decimal position – Samir Oct 27 '14 at 20:03
2

(float 411/125) is another variant if you are given the numbers directly, which is the case if you are just using the REPL as a calculator. Unfortunately this is a few characters longer than the solution by Jonathan Feinberg and ire_and_curses. ;)

user1460043
  • 2,331
  • 1
  • 19
  • 26