[Haskell-cafe] weired

Einar Karttunen ekarttun at cs.helsinki.fi
Sat Jul 16 04:14:39 EDT 2005


wenduan <xuwenduan2010 at gmail.com> writes:
> The following function which converts a number represents a sum of money 
> in pence didn't work as expected and the result didn't make any sense to me:
>
> penceToString :: Price -> String
> penceToString  p  =
>   let  str  =  show p
>        len  =  length str
>   in
>     if len ==1 then "0.0" ++ str else
>          if len ==2 then "0." ++ str else (take (len-2) str) ++ "." ++ 
> (drop (len - 2) str )
>
> *Main> penceToString 234566666667899999999786
> "-6710990.94"

You are encountering the fact the Int is a fixed size type (32 bits on
many common architectures). Thus 

*Main> 234566666667899999999786 :: Int
-671099094

Which explains the result. 

To make the program work use Integer instead of Int.


- Einar Karttunen


More information about the Haskell-Cafe mailing list