Created
February 1, 2015 03:38
-
-
Save kurogelee/e5cdfd8bd95b900fcbd4 to your computer and use it in GitHub Desktop.
任意の単位での切り捨て、切り上げ、四捨五入 ref: http://qiita.com/kurogelee/items/69b35e75b10dacf8be8b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn half [x] (/ x 2)) | |
(defn floor [value unit] | |
{:pre [(pos? unit)]} | |
(cond | |
(Double/isNaN value) value | |
(Double/isInfinite value) value | |
:else (let [q (quot value unit)] | |
(* unit (if (neg? (rem value unit)) | |
(dec q) q))))) | |
(defn ceil [value unit] | |
(- (floor (- value) unit))) | |
(defn round [value unit] | |
(floor (+ value (half unit)) unit)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(floor 3.7 2.5) ;2.5 | |
(ceil 3.7 2.5) ;5.0 | |
(round 3.7 2.5) ;2.5 | |
(round 3.8 2.5) ;5.0 | |
(floor 12.34 0.1);12.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment