Skip to content

Instantly share code, notes, and snippets.

@kurogelee
Created February 1, 2015 03:38
Show Gist options
  • Save kurogelee/e5cdfd8bd95b900fcbd4 to your computer and use it in GitHub Desktop.
Save kurogelee/e5cdfd8bd95b900fcbd4 to your computer and use it in GitHub Desktop.
任意の単位での切り捨て、切り上げ、四捨五入 ref: http://qiita.com/kurogelee/items/69b35e75b10dacf8be8b
(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))
(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