dev: clarify some amount operations
This commit is contained in:
parent
d435557979
commit
64ffdd7c9c
@ -325,7 +325,7 @@ transformAmount f a@Amount{aquantity=q,aprice=p} = a{aquantity=f q, aprice=f' <$
|
|||||||
f' (TotalPrice a1@Amount{aquantity=pq}) = TotalPrice a1{aquantity = f pq}
|
f' (TotalPrice a1@Amount{aquantity=pq}) = TotalPrice a1{aquantity = f pq}
|
||||||
f' p' = p'
|
f' p' = p'
|
||||||
|
|
||||||
-- | Divide an amount's quantity (and its total price, if it has one) by a constant.
|
-- | Divide an amount's quantity (and total cost, if any) by some number.
|
||||||
divideAmount :: Quantity -> Amount -> Amount
|
divideAmount :: Quantity -> Amount -> Amount
|
||||||
divideAmount n = transformAmount (/n)
|
divideAmount n = transformAmount (/n)
|
||||||
|
|
||||||
|
|||||||
@ -97,21 +97,25 @@ priceDirectiveToMarketPrice PriceDirective{..} =
|
|||||||
, mprate = aquantity pdamount
|
, mprate = aquantity pdamount
|
||||||
}
|
}
|
||||||
|
|
||||||
-- | Make one or more `MarketPrice` from an 'Amount' and its price directives.
|
-- | Infer a market price from the given amount and its cost (if any),
|
||||||
|
-- and make a corresponding price directive on the given date.
|
||||||
amountPriceDirectiveFromCost :: Day -> Amount -> Maybe PriceDirective
|
amountPriceDirectiveFromCost :: Day -> Amount -> Maybe PriceDirective
|
||||||
amountPriceDirectiveFromCost d amt@Amount{acommodity=fromcomm, aquantity=fromq} = case aprice amt of
|
amountPriceDirectiveFromCost d amt@Amount{acommodity=fromcomm, aquantity=n} = case aprice amt of
|
||||||
Just (UnitPrice pa) -> Just $ pd{pdamount=pa}
|
Just (UnitPrice u) -> Just $ pd{pdamount=u}
|
||||||
Just (TotalPrice pa) | fromq /= 0 -> Just $ pd{pdamount=fromq `divideAmountExtraPrecision` pa}
|
Just (TotalPrice t) | n /= 0 -> Just $ pd{pdamount=u} where u = divideAmountExtraPrecision n t}
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
where
|
where
|
||||||
pd = PriceDirective{pddate = d, pdcommodity = fromcomm, pdamount = nullamt}
|
pd = PriceDirective{pddate = d, pdcommodity = fromcomm, pdamount = nullamt}
|
||||||
|
|
||||||
divideAmountExtraPrecision n a = (n `divideAmount` a) { astyle = style' }
|
-- | Divide an amount's quantity (and total cost, if any) by some number n,
|
||||||
|
-- and also increase its display precision by the number of digits in n's integer part,
|
||||||
|
-- to avoid showing a misleadingly rounded result.
|
||||||
|
divideAmountExtraPrecision n a = (divideAmount n a) { astyle = style' }
|
||||||
where
|
where
|
||||||
style' = (astyle a) { asprecision = precision' }
|
style' = (astyle a) { asprecision = precision' }
|
||||||
precision' = case asprecision (astyle a) of
|
precision' = case asprecision (astyle a) of
|
||||||
NaturalPrecision -> NaturalPrecision
|
NaturalPrecision -> NaturalPrecision
|
||||||
Precision p -> Precision $ (numDigitsInt $ truncate n) + p
|
Precision p -> Precision $ p + numDigitsInt (truncate n)
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
-- Converting things to value
|
-- Converting things to value
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user