Reputation: 87
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!
Upvotes: 0
Views: 1058
Reputation: 477210
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 Num
erical type:
r :: Double -> Double
r = fromIntegral . round
For example:
Prelude> r 14.25
14.0
Upvotes: 1