0
votes

I am trying to round a variable using Haskell.

r :: Double -> Double
r x = round (x)

Trying to compile this, Im getting the following error

No instance for (Integral Double) arising from a use of 'round'

How can I fix this? Thank you!

1

1 Answers

1
votes

That is logical, since a type that is an instance of Integral, well..., is usually "integral". It thus means it is something that behave like an Integer. Examples of Integral, are Integer, Int, Word8, etc. A Float, Double, etc. is not integral, like the error says.

You can use fromIntegral :; (Num b, Integral a) => a -> b to convert something that is of an Integral type, to a Numerical type:

r :: Double -> Double
r = fromIntegral . round

For example:

Prelude> r 14.25
14.0