Scala Tutorial - Scala Numeric Types








The numeric data types in Scala constitute Float and Double types along with Integral data types such as Byte, Short, Int, Long, and Char.

The following table displays Scala's numeric data types.

Data typeDescription
ByteIntegers in the range from -128 to 127
ShortIntegers in the range from -32768 to 32767
IntIntegers in the range from -2147483648 to 2147483647
LongIntegers in the range from -9223372036854775808 to 9223372036854775807
FloatThe largest positive finite float is 3.4028235*1038 and thesmallest positive finite nonzero float is 1.40*10-45
DoubleThe largest positive finite double is 1.7976931348623157*10308 and the smallest positive finite nonzero double is 4.9*10-324




Example

Scala can automatically convert numbers from one type to another in the order.

Byte . Short . Int . Long . Float . Double. 

where the Byte type is the lowest and can be converted to any other type as illustrated in the following example:

val x: Byte = 30 

We can assign x to a Short type as illustrated in the following example:

val y: Short = x 

Likewise we can assign x to an Int, Long, Float, Double, and Scala will automatically convert the numbers for you as illustrated in the following example:

val z: Double = y 

Scala does not allow automatic conversion in the order reverse from mentioned earlier.