refactor: setAmountPrecision -> withPrecision

This commit is contained in:
Simon Michael 2012-11-19 23:24:04 +00:00
parent 9cd228329c
commit 45d4a38cc7
3 changed files with 10 additions and 10 deletions

View File

@ -537,8 +537,8 @@ tests_Hledger_Data_Amount = TestList $
(a3 + a3) `is` usd (-2.46) (a3 + a3) `is` usd (-2.46)
sum [a1,a2,a3,-a3] `is` usd 0 sum [a1,a2,a3,-a3] `is` usd 0
-- highest precision is preserved -- highest precision is preserved
let ap1 = setAmountPrecision 1 $ usd 1 let ap1 = usd 1 `withPrecision` 1
ap3 = setAmountPrecision 3 $ usd 1 ap3 = usd 1 `withPrecision` 3
(asprecision $ astyle $ sum [ap1,ap3]) `is` 3 (asprecision $ astyle $ sum [ap1,ap3]) `is` 3
(asprecision $ astyle $ sum [ap3,ap1]) `is` 3 (asprecision $ astyle $ sum [ap3,ap1]) `is` 3
-- adding different commodities assumes conversion rate 1 -- adding different commodities assumes conversion rate 1
@ -561,7 +561,7 @@ tests_Hledger_Data_Amount = TestList $
,"adding mixed amounts" ~: do ,"adding mixed amounts" ~: do
(sum $ map (Mixed . (\a -> [a])) (sum $ map (Mixed . (\a -> [a]))
[usd 1.25 [usd 1.25
,setAmountPrecision 0 $ usd (-1) ,usd (-1) `withPrecision` 0
,usd (-0.25) ,usd (-0.25)
]) ])
`is` Mixed [amount{aquantity=0}] `is` Mixed [amount{aquantity=0}]

View File

@ -296,8 +296,8 @@ balanceTransaction styles t@Transaction{tpostings=ps}
-- assign a balancing price. Use @@ for more exact output when possible. -- assign a balancing price. Use @@ for more exact output when possible.
-- invariant: prices should always be positive. Enforced with "abs" -- invariant: prices should always be positive. Enforced with "abs"
= if length ramountsinunpricedcommodity == 1 = if length ramountsinunpricedcommodity == 1
then TotalPrice $ setAmountPrecision maxprecision $ abs $ targetcommodityamount then TotalPrice $ abs targetcommodityamount `withPrecision` maxprecision
else UnitPrice $ setAmountPrecision maxprecision $ abs $ targetcommodityamount `divideAmount` (aquantity unpricedamount) else UnitPrice $ abs (targetcommodityamount `divideAmount` (aquantity unpricedamount)) `withPrecision` maxprecision
| otherwise = NoPrice | otherwise = NoPrice
where where
unpricedcommodity = head $ filter (`elem` (map acommodity rsumamounts)) rcommoditiesinorder unpricedcommodity = head $ filter (`elem` (map acommodity rsumamounts)) rcommoditiesinorder
@ -320,8 +320,8 @@ balanceTransaction styles t@Transaction{tpostings=ps}
where where
conversionprice c | c == unpricedcommodity conversionprice c | c == unpricedcommodity
= if length bvamountsinunpricedcommodity == 1 = if length bvamountsinunpricedcommodity == 1
then TotalPrice $ setAmountPrecision maxprecision $ abs $ targetcommodityamount then TotalPrice $ abs targetcommodityamount `withPrecision` maxprecision
else UnitPrice $ setAmountPrecision maxprecision $ abs $ targetcommodityamount `divideAmount` (aquantity unpricedamount) else UnitPrice $ abs (targetcommodityamount `divideAmount` (aquantity unpricedamount)) `withPrecision` maxprecision
| otherwise = NoPrice | otherwise = NoPrice
where where
unpricedcommodity = head $ filter (`elem` (map acommodity bvsumamounts)) bvcommoditiesinorder unpricedcommodity = head $ filter (`elem` (map acommodity bvsumamounts)) bvcommoditiesinorder
@ -444,7 +444,7 @@ tests_Hledger_Data_Transaction = TestList $ concat [
]) ])
(showTransaction (showTransaction
(txnTieKnot $ Transaction (parsedate "2010/01/01") Nothing False "" "x" "" [] (txnTieKnot $ Transaction (parsedate "2010/01/01") Nothing False "" "x" "" []
[Posting False "a" (Mixed [amt 1 `at` (setAmountPrecision 0 $ usd 2)]) "" RegularPosting [] Nothing [Posting False "a" (Mixed [amt 1 `at` (usd 2 `withPrecision` 0)]) "" RegularPosting [] Nothing
,Posting False "b" missingmixedamt "" RegularPosting [] Nothing ,Posting False "b" missingmixedamt "" RegularPosting [] Nothing
] "")) ] ""))
@ -480,7 +480,7 @@ tests_Hledger_Data_Transaction = TestList $ concat [
] "") ] "")
assertBool "balanceTransaction can infer conversion price" (isRight e) assertBool "balanceTransaction can infer conversion price" (isRight e)
assertEqual "balancing conversion price is inferred" assertEqual "balancing conversion price is inferred"
(Mixed [usd 1.35 @@ (setAmountPrecision maxprecision $ eur 1)]) (Mixed [usd 1.35 @@ (eur 1 `withPrecision` maxprecision)])
(case e of (case e of
Right e' -> (pamount $ head $ tpostings e') Right e' -> (pamount $ head $ tpostings e')
Left _ -> error' "should not happen") Left _ -> error' "should not happen")

View File

@ -577,7 +577,7 @@ amountp = try leftsymbolamount <|> try rightsymbolamount <|> nosymbolamount
tests_amountp = [ tests_amountp = [
"amountp" ~: do "amountp" ~: do
assertParseEqual (parseWithCtx nullctx amountp "$47.18") (usd 47.18) assertParseEqual (parseWithCtx nullctx amountp "$47.18") (usd 47.18)
assertParseEqual (parseWithCtx nullctx amountp "$1.") (setAmountPrecision 0 $ usd 1) assertParseEqual (parseWithCtx nullctx amountp "$1.") (usd 1 `withPrecision` 0)
,"amount with unit price" ~: do ,"amount with unit price" ~: do
assertParseEqual assertParseEqual
(parseWithCtx nullctx amountp "$10 @ €0.5") (parseWithCtx nullctx amountp "$10 @ €0.5")