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

倾然丶 夕夏残阳落幕 提交于 2019-11-30 22:17:46

问题


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


回答1:


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



回答2:


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



回答3:


As documented, integer division yields rational numbers. Try

(/ 411.0 125)



回答4:


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.




回答5:


even this will work:

(/ 22. 7) => 3.142857142857143



回答6:


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



来源:https://stackoverflow.com/questions/1596484/when-i-divide-numbers-in-clojure-i-get-a-fraction-how-do-i-get-the-decimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!