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 type | Description |
---|---|
Byte | Integers in the range from -128 to 127 |
Short | Integers in the range from -32768 to 32767 |
Int | Integers in the range from -2147483648 to 2147483647 |
Long | Integers in the range from -9223372036854775808 to 9223372036854775807 |
Float | The largest positive finite float is 3.4028235*1038 and thesmallest positive finite nonzero float is 1.40*10-45 |
Double | The largest positive finite double is 1.7976931348623157*10308 and the smallest positive finite nonzero double is 4.9*10-324 |
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.