Numeric Conversions and Operations
Explore Kotlin's number handling including literal formatting, numeral systems, and type conversions. Learn arithmetic and bitwise operations, and when to use BigDecimal and BigInteger for precise calculations.
Underscores in numbers
In number literals, we can use the underscore (_) between digits. This character is ignored, but we sometimes use it to format long numbers for better readability.
Other numeral systems
To define a number using the hexadecimal numeral system, start with 0x. To define a number using the binary numeral system, start with 0b. The octal numeral system is not supported.
The Number type and conversion functions
All basic types that represent numbers are subtypes of the Number type.
The Number type specifies transformation functions: from the current number to any other basic type representing a number.
This means that we can transform each basic number into a different one using the to{new type} function. Such functions are known as conversion functions.
Operations on numbers
Numbers in Kotlin support the basic mathematical operations: ...