[Haskell-cafe] Fractional Part
valheru at chariot.net.au
Wed Aug 3 01:36:46 CEST 2011
You could write the following
decimalPart :: Float -> Integer
decimalPart f = read (tail (tail (show (f)))) :: Integer
This basically says "convert f into a String using the show function, and then get the tail of that String twice to get rid of the leading zero and the decimal point then read the result back as an Integer".
To use this function you would type at the prompt:
This is probably not a very elegant solution, but it will work.
On 03/08/2011, at 8:36 AM, Ata Jafari wrote:
> Hi everyone,
> I'm totally new to Haskell and functional programming. I try to solve some problems from Proejct Euler with Haskell so that I can improve myself in functional programming.
> In the first step I want to write a little code that can give me only the decimal part of a float. For instance:
> if the number is (0.123) I want to obtain only the (123) part to do some processes on that. (I mean the result of this code must be an integer.)
> I don't expect any complete code, any suggestion and help will be appreciated.
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe