]> Git — Sourcephile - comptalang.git/blob - lib/Hcompta/Amount/Quantity.hs
Ajout : CLI.Command.{Journals,Stats,Tags}.
[comptalang.git] / lib / Hcompta / Amount / Quantity.hs
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE StandaloneDeriving #-}
5 {-# OPTIONS_GHC -fno-warn-orphans #-}
6 module Hcompta.Amount.Quantity where
7
8 import Data.Data
9 #ifdef DOUBLE
10 #else
11 import Data.Decimal
12 #endif
13 import Data.Word
14 import Data.Typeable ()
15
16 #ifdef DOUBLE
17 type Quantity
18 = Double
19 #else
20 type Quantity
21 = Decimal
22 deriving instance Data Quantity
23 #endif
24
25 representation :: String
26 zero :: Quantity
27 is_zero :: Quantity -> Bool
28 round :: Word8 -> Quantity -> Quantity
29 #ifdef DOUBLE
30 representation = "Double"
31 zero = 0.0
32
33 round n f = fromInteger $ round $ (f * (10^n)) / (10.0^^n)
34
35 is_zero = (== 0) . decimalMantissa
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 zero = 0
45
46 round = Data.Decimal.roundTo
47
48 is_zero = (== 0) . decimalMantissa
49 --is_zero decimal_places quantity =
50 -- (== 0) $ decimalMantissa $
51 -- Hcompta.Amount.Quantity.round decimal_places quantity
52 #endif
53