@@ -5,8 +5,11 @@ module Data.Finance.Money
55
66import Data.Finance.Currency (kind Currency, class Currency, CProxy(..), decimals)
77import Data.Generic (class Generic, gShow)
8+import Data.Int as Int
89import Data.Module (class LeftModule, class RightModule)
910import Data.Newtype (class Newtype)
11+import Global.Unsafe (unsafeToFixed)
12+import Math (pow)
1013import Prelude
1114
1215-- | An amount of money in the smallest discrete unit of a particular currency.
@@ -32,7 +35,10 @@ instance rightModuleDiscrete :: RightModule (Discrete c) Int where
3235 msubR (Discrete a) (Discrete b) = Discrete (a - b)
3336 mmulR (Discrete a) b = Discrete (a * b)
3437
38+-- | Show the discrete value with the correct number of decimals. Will not
39+-- | prepend the currency sign. Negative numbers are prefixed with a
40+-- | hyphen-minus.
3541showDiscrete :: ∀ c. (Currency c) => Discrete c -> String
36−showDiscrete (Discrete n) = showDiscrete' n (decimals (CProxy :: CProxy c))
37−
38−foreign import showDiscrete' :: Int -> Int -> String
42+showDiscrete (Discrete n) =
43+ unsafeToFixed d (Int.toNumber n / pow 10.0 (Int.toNumber d))
44+ where d = decimals (CProxy :: CProxy c)