Numeric Data Types (Scala 3 Video)
Scala’s numeric data types:
Byte
Short
Int
Long
Float
Double
BigInt
BigDecimal
Int
and Double
are the defaults:
val a = 1 // Int
val b = 2.2 // Double
Examples of how to create them:
val a = 1_000
val a: Long = 1_000
val a = 1_000L
val a: Byte = 1
val b: Short = 1
val c: Int = 1
val d: Long = 1
val e: Float = 1
val f: Double = 1
val bi = BigInt(1_234_567)
val bd = BigDecimal(1_234_567.123)
The types really only make sense if you know their ranges:
DATA TYPE DEFINITION
Byte 8-bit signed two's complement integer (-2^7 to 2^7-1, inclusive)
-128 to 127
Short 16-bit signed two's complement integer (-2^15 to 2^15-1, inclusive)
-32,768 to 32,767
Int 32-bit two's complement integer (-2^31 to 2^31-1, inclusive)
-2,147,483,648 to 2,147,483,647
Long 64-bit two's complement integer (-2^63 to 2^63-1, inclusive)
-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
Float 32-bit IEEE 754 single-precision float
1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative)
Double 64-bit IEEE 754 double-precision float
4.94065645841246544e-324 to 1.79769313486231570e+308 (positive or negative)
Char 16-bit unsigned Unicode character (0 to 2^16-1, inclusive)
0 to 65,535
Currency:
- Most devs use
BigDecimal
orLong
for currency (in addition to libraries)
Update: All of my new videos are now on
LearnScala.dev