dev: Amount: cleanups
This commit is contained in:
parent
e035730afb
commit
1ae59ad0af
@ -343,6 +343,7 @@ multiplyAmount :: Quantity -> Amount -> Amount
|
|||||||
multiplyAmount n = transformAmount (*n)
|
multiplyAmount n = transformAmount (*n)
|
||||||
|
|
||||||
-- | Invert an amount (replace its quantity q with 1/q).
|
-- | Invert an amount (replace its quantity q with 1/q).
|
||||||
|
-- (Its cost if any is not changed, currently.)
|
||||||
invertAmount :: Amount -> Amount
|
invertAmount :: Amount -> Amount
|
||||||
invertAmount a@Amount{aquantity=q} = a{aquantity=1/q}
|
invertAmount a@Amount{aquantity=q} = a{aquantity=1/q}
|
||||||
|
|
||||||
@ -383,6 +384,13 @@ amountIsZero = testAmountAndTotalPrice (\Amount{aquantity=Decimal _ q} -> q == 0
|
|||||||
-- representing an infinite decimal ?
|
-- representing an infinite decimal ?
|
||||||
amountHasMaxDigits :: Amount -> Bool
|
amountHasMaxDigits :: Amount -> Bool
|
||||||
amountHasMaxDigits = (>= 255) . numDigitsInteger . decimalMantissa . aquantity
|
amountHasMaxDigits = (>= 255) . numDigitsInteger . decimalMantissa . aquantity
|
||||||
|
-- XXX this seems not always right. Eg:
|
||||||
|
-- ghci> let n = 100 / (3.0 :: Decimal)
|
||||||
|
-- decimalPlaces n
|
||||||
|
-- 255
|
||||||
|
-- numDigitsInteger $ decimalMantissa n
|
||||||
|
-- 257
|
||||||
|
|
||||||
|
|
||||||
-- | Set an amount's display precision, flipped.
|
-- | Set an amount's display precision, flipped.
|
||||||
withPrecision :: Amount -> AmountPrecision -> Amount
|
withPrecision :: Amount -> AmountPrecision -> Amount
|
||||||
@ -456,20 +464,17 @@ amountDisplayPrecision a =
|
|||||||
Precision n -> n
|
Precision n -> n
|
||||||
NaturalPrecision -> amountInternalPrecision a
|
NaturalPrecision -> amountInternalPrecision a
|
||||||
|
|
||||||
-- | Set an amount's internal precision, ie rounds the Decimal representing
|
-- | Set an amount's internal decimal precision as well as its display precision.
|
||||||
-- the amount's quantity to some number of decimal places.
|
-- This rounds or pads its Decimal quantity to the specified number of decimal places.
|
||||||
-- Rounding is done with Data.Decimal's default roundTo function:
|
-- Rounding is done with Data.Decimal's default roundTo function:
|
||||||
-- "If the value ends in 5 then it is rounded to the nearest even value (Banker's Rounding)".
|
-- "If the value ends in 5 then it is rounded to the nearest even value (Banker's Rounding)".
|
||||||
-- Does not change the amount's display precision.
|
|
||||||
-- Intended mainly for internal use, eg when comparing amounts in tests.
|
|
||||||
setAmountInternalPrecision :: Word8 -> Amount -> Amount
|
setAmountInternalPrecision :: Word8 -> Amount -> Amount
|
||||||
setAmountInternalPrecision p a@Amount{ aquantity=q, astyle=s } = a{
|
setAmountInternalPrecision p a@Amount{ aquantity=q, astyle=s } = a{
|
||||||
astyle=s{asprecision=Precision p}
|
aquantity=roundTo p q
|
||||||
,aquantity=roundTo p q
|
,astyle=s{asprecision=Precision p}
|
||||||
}
|
}
|
||||||
|
|
||||||
-- | Set an amount's internal precision, flipped.
|
-- | setAmountInternalPrecision with arguments flipped.
|
||||||
-- Intended mainly for internal use, eg when comparing amounts in tests.
|
|
||||||
withInternalPrecision :: Amount -> Word8 -> Amount
|
withInternalPrecision :: Amount -> Word8 -> Amount
|
||||||
withInternalPrecision = flip setAmountInternalPrecision
|
withInternalPrecision = flip setAmountInternalPrecision
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user