]> Git — Sourcephile - comptalang.git/blob - lib/Hcompta/Model/Amount/Quantity.hs
Ajout : CLI.Command.Balance : affiche Calc.Balance.Amount_Sum.
[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 zero :: Quantity
28 is_zero :: Quantity -> Bool
29 round :: Word8 -> Quantity -> Quantity
30 #ifdef DOUBLE
31 representation = "Double"
32 zero = 0.0
33
34 round n f = fromInteger $ round $ (f * (10^n)) / (10.0^^n)
35
36 is_zero = (== 0) . decimalMantissa
37 --is_zero decimal_places quantity =
38 -- floor quantity == 0 && -- NOTE: check integral part, in case of an overflow in roundTo'
39 -- 0 == roundTo' decimal_places quantity
40 -- where
41 -- roundTo' n f = fromInteger $ round $ f * (10 ^ n)
42
43 #else
44 representation = "Decimal"
45 zero = fromInteger 0
46
47 round = Data.Decimal.roundTo
48
49 is_zero = (== 0) . decimalMantissa
50 --is_zero decimal_places quantity =
51 -- (== 0) $ decimalMantissa $
52 -- Hcompta.Model.Amount.Quantity.round decimal_places quantity
53 #endif
54