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

MathClojureFloating PointNumbers

Math Problem Overview


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

Math Solutions


Solution 1 - Math

user> (float (/ 411 125))
3.288
user> (double (/ 411 125))
3.288

Solution 2 - Math

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

Solution 3 - Math

As documented, integer division yields rational numbers. Try

(/ 411.0 125)

Solution 4 - Math

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.

Solution 5 - Math

even this will work:

(/ 22. 7) => 3.142857142857143

Solution 6 - Math

(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. ;)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionunj2View Question on Stackoverflow
Solution 1 - MathBrian CarperView Answer on Stackoverflow
Solution 2 - MathJacek LaskowskiView Answer on Stackoverflow
Solution 3 - MathJonathan FeinbergView Answer on Stackoverflow
Solution 4 - Mathire_and_cursesView Answer on Stackoverflow
Solution 5 - MathSamirView Answer on Stackoverflow
Solution 6 - Mathuser1460043View Answer on Stackoverflow