It depends on what you have. If a is an Integral instance like Int or Integer , you can use
fromIntegral :: (Integral a, Num b) => a -> b
If a is an instance of Real, such as Rational or Double, you can use
realToFrac :: (Real a, Fractional b) => a -> b
There are also special functions for Integer and Rational, which fromIntegral and realToFrac are based on:
fromInteger :: (Num a) => Integer -> a fromRational :: (Fractional a) => Rational -> a
If a is an instance of Enum (and therefore you can use fromEnum , as you said), then fromIntegral . fromEnum fromIntegral . fromEnum has to do this. fromEnum converts a to Int , then fromIntegral converts from Int to Float , so fromIntegral . fromEnum fromIntegral . fromEnum converts a to Float .
ehird source share