[Haskell-cafe] money type ?

Bayley, Alistair Alistair_Bayley at invescoperpetual.co.uk
Thu Jul 12 04:58:58 EDT 2007


> From: Brandon S. Allbery KF8NH [mailto:allbery at ece.cmu.edu] 
> 
> 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
> >
> > ...
> > *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?

Yes... I'm now wondering how one would read Money values. There's a Show instance for Fixed, but no Read. Could use:

readMoney :: String -> Money
readMoney s = let d :: Double; d = read s in realToFrac d

... but is there a better way? Note that readMoney is a bit dumb:

*Main> readMoney "3.149999"
3.14

Alistair
*****************************************************************
Confidentiality Note: The information contained in this message,
and any attachments, may contain confidential and/or privileged
material. It is intended solely for the person(s) or entity to
which it is addressed. Any review, retransmission, dissemination,
or taking of any action in reliance upon this information by
persons or entities other than the intended recipient(s) is
prohibited. If you received this in error, please contact the
sender and delete the material from any computer.
*****************************************************************


More information about the Haskell-Cafe mailing list