[Haskell-cafe] money type ?
Brandon S. Allbery KF8NH
allbery at ece.cmu.edu
Thu Jul 12 04:45:57 EDT 2007
On Jul 12, 2007, at 4:40 , Bayley, Alistair wrote:
>> From: haskell-cafe-bounces at haskell.org
>> [mailto:haskell-cafe-bounces at haskell.org] On Behalf Of Simon Michael
>>
>> Is there a type or library out there that's good for
>> representing money and
>> other quantities while avoiding rounding errors ?
>
> I think Data.Fixed would be a good choice:
>
> http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-
> Fixed.ht
> ml
>
> I don't know /exactly/ how you'd go about adding a Money data type; I
> guess you'd have something like:
>
>> data E2 = E2
>> instance HasResolution E2 where resolution _ = 100
>> type Money = Fixed E2
I was playing with that when your message came in:
> mress:5003 Z$ cat foo.hs
> import Data.Fixed
>
> data E2 = E2
> instance HasResolution E2 where
> resolution _ = 100
> type Money = Fixed E2
>
> mress:5004 Z$ ghci foo.hs
> ___ ___ _
> / _ \ /\ /\/ __(_)
> / /_\// /_/ / / | | GHC Interactive, version 6.7.20070322,
> for Haskell 98.
> / /_\\/ __ / /___| | http://www.haskell.org/ghc/
> \____/\/ /_/\____/|_| Type :? for help.
>
> Loading package base ... linking ... done.
> [1 of 1] Compiling Main ( foo.hs, interpreted )
> Ok, modules loaded: Main.
> *Main> :m +Data.List
> *Main Data.List> let l = [1.25 :: Money,2.00,4.46,12.80,1.15,6.00]
> in sum l / genericLength l
> 4.61
That what you want?
--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university KF8NH
More information about the Haskell-Cafe
mailing list