Sling Academy
Home/Kotlin/Finding Absolute Values in Kotlin (`abs`)

Finding Absolute Values in Kotlin (`abs`)

Last updated: November 30, 2024

The absolute value of a number is its non-negative value irrespective of its sign. In Kotlin, you can simply find the absolute value using the abs function. Kotlin provides this functionality for various number types, including Int, Double, Float, and Long.

Using the abs() Function

The abs() function is part of the Kotlin Standard Library. Depending on the type of number (such as Int, Double, etc.), Kotlin automatically chooses the correct abs() implementation to ensure precision.

Example with Int

import kotlin.math.abs

fun main() {
    val number: Int = -10
    val absoluteValue = abs(number)
    println("Absolute value of -10 is: " + absoluteValue)
}

In this example, abs() takes an Int, and returns the absolute value of -10, which is 10.

Example with Double

import kotlin.math.abs

fun main() {
    val number: Double = -3.14
    val absoluteValue = abs(number)
    println("Absolute value of -3.14 is: " + absoluteValue)
}

This is another example where abs() handles a Double type, returning 3.14 for the input -3.14.

Example in a Function

The abs() function can also be used when defining your own functions:

import kotlin.math.abs

fun getAbsoluteValue(number: Int): Int {
    return abs(number)
}

fun main() {
    val number: Int = -25
    println("Absolute value of -25 is: ${getAbsoluteValue(number)}")
}

This example demonstrates how you can wrap the abs() function within another function, adding more specific functionality or simplifying calls in your code.

Conclusion

Leveraging the abs() function in Kotlin is straightforward. It enhances code readability and ensures that you're obtaining absolute values with precision. Keeping in mind the data type you are working with will help you use abs() more effectively across your applications.

Next Article: Generating Random Numbers in Kotlin

Previous Article: Calculating Square Roots in Kotlin (`sqrt`)

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