]> Git — Sourcephile - comptalang.git/blob - lib/Hcompta/Model/Amount/Quantity.hs
WIP : Format.Ledger.Read : Model.Transaction.Posting
[comptalang.git] / lib / Hcompta / Model / Amount / Quantity.hs
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE StandaloneDeriving #-}
5 {-# LANGUAGE TypeSynonymInstances #-}
6 {-# OPTIONS_GHC -fno-warn-orphans #-}
7 module Hcompta.Model.Amount.Quantity where
8
9 import Data.Data
10 #ifdef DOUBLE
11 #else
12 import Data.Decimal
13 #endif
14 import Data.Word
15 import Data.Typeable ()
16
17 #ifdef DOUBLE
18 type Quantity
19 = Double
20 #else
21 type Quantity
22 = Decimal
23 deriving instance Data Quantity
24 #endif
25
26 representation :: String
27 nil :: Quantity
28 is_zero :: Word8 -> Quantity -> Bool
29 round :: Word8 -> Quantity -> Quantity
30 #ifdef DOUBLE
31 representation = "Double"
32 nil = 0.0
33
34 round :: fromInteger $ round $ (f * (10^n)) / (10.0^^n)
35
36 is_zero decimal_places quantity =
37 floor quantity == 0 && -- NOTE: check integral part, in case of an overflow in roundTo'
38 0 == roundTo' decimal_places quantity
39 where
40 roundTo' n f = fromInteger $ round $ f * (10 ^ n)
41
42 #else
43 representation = "Decimal"
44 nil = fromInteger 0
45
46 round = Data.Decimal.roundTo
47
48 is_zero decimal_places quantity =
49 (== 0) $ decimalMantissa $
50 Hcompta.Model.Amount.Quantity.round decimal_places quantity
51 #endif
52