{-# LANGUAGE ScopedTypeVariables #-}

module Data.Fixed.Extras where

import Data.Fixed
import Prelude

-- | Round to fixed precision using a rounding function.
--
-- >>> toFixed ceiling 0.12345 :: Milli
-- 0.124
toFixed :: forall res a. (Num a, HasResolution res) => (a -> Integer) -> a -> Fixed res
toFixed :: forall res a.
(Num a, HasResolution res) =>
(a -> Integer) -> a -> Fixed res
toFixed a -> Integer
rounding a
a =
  let r :: Integer
r = forall k (a :: k) (p :: k -> *). HasResolution a => p a -> Integer
resolution Fixed res
out
      out :: Fixed res
out = forall k (a :: k). Integer -> Fixed a
MkFixed forall a b. (a -> b) -> a -> b
$ a -> Integer
rounding (a
a forall a. Num a => a -> a -> a
* forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
r)
   in Fixed res
out