{-# 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 = Fixed res -> Integer
forall k (a :: k) (p :: k -> *). HasResolution a => p a -> Integer
forall (p :: * -> *). p res -> Integer
resolution Fixed res
out
      out :: Fixed res
out = Integer -> Fixed res
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> Fixed res) -> Integer -> Fixed res
forall a b. (a -> b) -> a -> b
$ a -> Integer
rounding (a
a a -> a -> a
forall a. Num a => a -> a -> a
* Integer -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
r)
   in Fixed res
out