When I divide the numbers in clojure, I get a fraction, how can I get the decimal number?

When I do (/ 411 125) , I do not get it in terms of decimal. How to do it?

+62
math floating-point numbers clojure
Oct 20 '09 at 18:32
source share
6 answers
 user> (float (/ 411 125)) 3.288 user> (double (/ 411 125)) 3.288 
+77
Oct. 20 '09 at 18:42
source share
 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 
+27
Aug 10 2018-12-12T00:
source share

As documented , integer division gives rational numbers. Try

 (/ 411.0 125) 
+12
Oct. 20 '09 at 18:36
source share

If you use float for dividends, you will get a decimal answer.

 (/ 22.0 7) -> 3.142857142857143 

A function is also available (unchecked remainder xy) .

+6
Oct 20 '09 at 18:38
source share

even this will work:

 (/ 22. 7) => 3.142857142857143 
+3
Oct 26 '14 at 21:01
source share

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

+1
Sep 24 '14 at 2:11
source share



All Articles