packages feed

national-australia-bank 0.0.8 → 0.0.9

raw patch · 3 files changed

+126/−15 lines, 3 files

Files

changelog.md view
@@ -1,3 +1,7 @@+0.0.9++* Fix `amountReal` to accept function how to handle precision loss.+ 0.0.8  * Add `amountReal`
national-australia-bank.cabal view
@@ -1,5 +1,5 @@ name:                 national-australia-bank-version:              0.0.8+version:              0.0.9 synopsis:             Functions for National Australia Bank transactions description:          Parsing, Processing and other functions for National Australia Bank transactions license:              BSD3
src/Data/Bank/NationalAustraliaBank/NationalAustraliaBank.hs view
@@ -41,7 +41,7 @@ import Data.Validation ( toEither ) import Data.Time ( fromGregorian, toGregorian, diffDays, Day ) import Data.Functor.Contravariant ( Contravariant(contramap) )-import Data.Ratio ( Ratio, (%), numerator )+import Data.Ratio ( Ratio, (%) ) import System.Directory     ( doesDirectoryExist,       listDirectory )@@ -460,33 +460,140 @@  -- | ----- >>> amountReal 0+-- >>> amountRealRound 0 -- Amount {_negated = False, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit0} ----- >>> amountReal 0.01+-- >>> amountRealRound 0.01 -- Amount {_negated = False, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit1} ----- >>> amountReal 0.0123--- Amount {_negated = False, _dollars = DecDigit1 :| [], _cents1 = DecDigit2, _cents2 = DecDigit3}+-- >>> amountRealRound 0.0123+-- Amount {_negated = False, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit1} ----- >>> amountReal (-0.01)+-- >>> amountRealRound (-0.01) -- Amount {_negated = True, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit1} ----- >>> amountReal (-0.0123)--- Amount {_negated = True, _dollars = DecDigit1 :| [], _cents1 = DecDigit2, _cents2 = DecDigit3}+-- >>> amountRealRound (-0.0123)+-- Amount {_negated = True, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit1} ----- >>> amountReal (-12.01)+-- >>> amountRealRound (-12.01) -- Amount {_negated = True, _dollars = DecDigit1 :| [DecDigit2], _cents1 = DecDigit0, _cents2 = DecDigit1} ----- >>> > amountReal (452.99)+-- >>> amountRealRound (452.99) -- Amount {_negated = False, _dollars = DecDigit4 :| [DecDigit5,DecDigit2], _cents1 = DecDigit9, _cents2 = DecDigit9}-amountReal ::+--+-- >>> amountRealRound (391233 % 67700)+-- Amount {_negated = False, _dollars = DecDigit5 :| [], _cents1 = DecDigit7, _cents2 = DecDigit8}+amountRealRound ::   Integral a =>   Ratio a   -> Amount-amountReal r =+amountRealRound =+  amountReal round++-- |+--+-- >>> amountRealTruncate 0+-- Amount {_negated = False, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit0}+--+-- >>> amountRealTruncate 0.01+-- Amount {_negated = False, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit1}+--+-- >>> amountRealTruncate 0.0123+-- Amount {_negated = False, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit1}+--+-- >>> amountRealTruncate (-0.01)+-- Amount {_negated = True, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit1}+--+-- >>> amountRealTruncate (-0.0123)+-- Amount {_negated = True, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit1}+--+-- >>> amountRealTruncate (-12.01)+-- Amount {_negated = True, _dollars = DecDigit1 :| [DecDigit2], _cents1 = DecDigit0, _cents2 = DecDigit1}+--+-- >>> amountRealTruncate (452.99)+-- Amount {_negated = False, _dollars = DecDigit4 :| [DecDigit5,DecDigit2], _cents1 = DecDigit9, _cents2 = DecDigit9}+--+-- >>> amountRealTruncate (391233 % 67700)+-- Amount {_negated = False, _dollars = DecDigit5 :| [], _cents1 = DecDigit7, _cents2 = DecDigit7}+amountRealTruncate ::+  Integral a =>+  Ratio a+  -> Amount+amountRealTruncate =+  amountReal truncate++-- |+--+-- >>> amountRealCeiling 0+-- Amount {_negated = False, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit0}+--+-- >>> amountRealCeiling 0.01+-- Amount {_negated = False, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit1}+--+-- >>> amountRealCeiling 0.0123+-- Amount {_negated = False, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit2}+--+-- >>> amountRealCeiling (-0.01)+-- Amount {_negated = True, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit1}+--+-- >>> amountRealCeiling (-0.0123)+-- Amount {_negated = True, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit1}+--+-- >>> amountRealCeiling (-12.01)+-- Amount {_negated = True, _dollars = DecDigit1 :| [DecDigit2], _cents1 = DecDigit0, _cents2 = DecDigit1}+--+-- >>> amountRealCeiling (452.99)+-- Amount {_negated = False, _dollars = DecDigit4 :| [DecDigit5,DecDigit2], _cents1 = DecDigit9, _cents2 = DecDigit9}+--+-- >>> amountRealCeiling (391233 % 67700)+-- Amount {_negated = False, _dollars = DecDigit5 :| [], _cents1 = DecDigit7, _cents2 = DecDigit8}+amountRealCeiling ::+  Integral a =>+  Ratio a+  -> Amount+amountRealCeiling =+  amountReal ceiling++-- |+--+-- >>> amountRealFloor 0+-- Amount {_negated = False, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit0}+--+-- >>> amountRealFloor 0.01+-- Amount {_negated = False, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit1}+--+-- >>> amountRealFloor 0.0123+-- Amount {_negated = False, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit1}+--+-- >>> amountRealFloor (-0.01)+-- Amount {_negated = True, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit1}+--+-- >>> amountRealFloor (-0.0123)+-- Amount {_negated = True, _dollars = DecDigit0 :| [], _cents1 = DecDigit0, _cents2 = DecDigit2}+--+-- >>> amountRealFloor (-12.01)+-- Amount {_negated = True, _dollars = DecDigit1 :| [DecDigit2], _cents1 = DecDigit0, _cents2 = DecDigit1}+--+-- >>> amountRealFloor (452.99)+-- Amount {_negated = False, _dollars = DecDigit4 :| [DecDigit5,DecDigit2], _cents1 = DecDigit9, _cents2 = DecDigit9}+--+-- >>> amountRealFloor (391233 % 67700)+-- Amount {_negated = False, _dollars = DecDigit5 :| [], _cents1 = DecDigit7, _cents2 = DecDigit7}+amountRealFloor ::+  Integral a =>+  Ratio a+  -> Amount+amountRealFloor =+  amountReal floor++amountReal ::+  Num a =>+  (a -> Integer)+  -> a+  -> Amount+amountReal loss r =   let div10 x = over _2 (fromMaybe DecDigit0 . preview integralDecimal) (divMod x 10)-      n = numerator (r * 100)+      n = loss (r * 100) :: Integer       (as1, c2) = div10 (abs n)       (as2, c1) = div10 as1   in  Amount (n < 0) (either id id (integralDecDigits as2)) c1 c2@@ -521,7 +628,7 @@ -- Amount {_negated = False, _dollars = DecDigit2 :| [DecDigit2], _cents1 = DecDigit3, _cents2 = DecDigit2} instance Semigroup Amount where   a1 <> a2 =-    amountReal (realAmount a1 + realAmount a2 :: Ratio Integer)+    amountRealTruncate (realAmount a1 + realAmount a2 :: Ratio Integer)  instance Monoid Amount where   mempty =