Kotlin Math.abs - Syntax
mixed abs(mixed n)
The Math.abs() function in Kotlin is used to return the absolute value or modulus |x| of a real number x is the non-negative value of x without regard to its sign.
mixed abs(mixed n)
mixed n |
Specifies the real number (int, long, float, double) / infinity (*Required) |
---|---|
Return mixed |
Returns a number / infinity respective to an input argument. |
Convert the positive and negative integer to an absolute value.
fun main(args: Array<String>) {
// Positive integer value
val n = Math.abs(9)
println("The absolute value of 9 is " + n)
// Negative integer value
val x = Math.abs(-6)
println("The absolute value of -6 is " + x)
}
Convert the positive and negative float to an absolute value.
fun main(args: Array<String>) {
// Positive floating point value
val n = Math.abs(9.3)
println("The absolute value of 9.3 is " + n)
// Negative floating point value
val x = Math.abs(-6.3)
println("The absolute value of -6.3 is " + x)
}
Convert the positive and negative Infinity to an absolute value.
import kotlin.Double.Companion
fun main(args: Array<String>) {
// Positive ∞ infinite value
val n = Math.abs(Companion.POSITIVE_INFINITY)
println("The absolute value of Infinity is " + n)
// Negative ∞ infinite value
val x = Math.abs(Companion.NEGATIVE_INFINITY)
println("The absolute value of -Infinity is " + x)
}