Sling Academy
Home/Kotlin/Working with Integers and Floating-Point Numbers in Kotlin

Working with Integers and Floating-Point Numbers in Kotlin

Last updated: December 05, 2024

Introduction

Kotlin is a modern programming language that offers powerful features and syntax for working with various data types. One of the key aspects of any programming language is its ability to handle numbers effectively. In Kotlin, numbers are broadly categorized into integers and floating-point numbers. Integers are numbers without fractional parts, while floating-point numbers allow for decimal values. In this article, we will explore how to work with both integers and floating-point numbers in Kotlin, and provide practical examples to better understand their usage.

Integers in Kotlin

Integers are whole numbers without a fractional component. Kotlin supports several types of integers, primarily differentiated by the size and range of values they can store. The primary integer types in Kotlin are:

  • Byte: 8 bits, range from -128 to 127
  • Short: 16 bits, range from -32,768 to 32,767
  • Int: 32 bits, range from -2,147,483,648 to 2,147,483,647
  • Long: 64 bits, range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Let’s see some examples of how to declare and use these integers in Kotlin:


val myByte: Byte = 100
val myShort: Short = 30000
val myInt: Int = 123456789
val myLong: Long = 123456789101112L

Notice the inclusion of the L postfix with Long values to denote a long integer. Without the L, Kotlin would assume an Int type.

Floating-Point Numbers in Kotlin

Floating-point numbers are used to represent numbers with a fractional part. Kotlin provides two principal floating-point types:

  • Float: 32 bits, single precision, with a range of approximately ±3.40282347E+38F (7 digits of precision)
  • Double: 64 bits, double precision, with a range of approximately ±1.7976931348623157E+308 (15 digits of precision)

Here's how you can define floating-point numbers in Kotlin:


val myFloat: Float = 98.6F
val myDouble: Double = 123.456789

It's crucial to append an 'F' to floating-point literals to indicate a Float; otherwise, Kotlin will interpret it as a Double by default.

Arithmetic Operations

Kotlin provides standard arithmetic operations, such as addition, subtraction, multiplication, and division, which can be performed both on integers and floating-point numbers. We can directly perform these operations using operators, as illustrated below:


val sumInt = myInt + 1000
val productFloat = myFloat * 2.5F

val divisionDouble = myDouble / 1.2
val subtractionLong = myLong - 123456L

Kotlin also supports complex operations using methods from the Math library:


val result = Math.pow(myDouble, 2.0)
val squareRoot = Math.sqrt(25.0)

Type Conversion

In Kotlin, you often need to convert numbers from one type to another. This can be accomplished using conversion methods such as toInt(), toDouble(), toFloat(), etc. Here are some examples:


val doubleValue = 42.98
val intValue = doubleValue.toInt()
println(intValue) // Prints: 42

val longValue = myInt.toLong()
println(longValue) // Prints: 123456789

It's important to be aware that conversion can lead to data loss. For instance, converting a Double to an Int truncates the decimal, as shown above.

Conclusion

Kotlin provides robust support for handling both integers and floating-point numbers, allowing developers to perform a vast range of mathematical computations efficiently. Understanding these types and how to use them effectively is fundamental for any Kotlin programmer. By using the functionalities covered in this article, you can confidently manage numeric data in your Kotlin applications.

Next Article: How to Use Long, Short, and Byte Types in Kotlin

Previous Article: Understanding Numbers in Kotlin

Series: Primitive data types in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin