Numbers

Integral types (integer-like types)

Integral types contain only whole numbers and not fractions. The most commonly used integral types are:

The workhorse for converting from integral types is fromIntegral, which will convert from any Integral type into any Numeric type (which includes Int, Integer, Rational, and Double):

def fromIntegral[A: Integral, B: Num](a: A): B

For example, given an Int value n, one does not simply take its square root by typing sqrt(n), since sqrt can only be applied to Floating-point numbers. Instead, one must write sqrt(fromIntegral(n)) to explicitly convert n to a floating-point number. There are special cases for converting from Integers:

def fromInteger[A: Integral](n: Int): A

as well as for converting to Integers:

def toInteger[A: Integral](n: Int): A