简体   繁体   中英

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

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

user> (float (/ 411 125))
3.288
user> (double (/ 411 125))
3.288
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

As documented , integer division yields rational numbers. Try

(/ 411.0 125)

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 xy) function available.

即使这样也会奏效:

(/ 22. 7) => 3.142857142857143

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM