Scala Tutorial Scala References

Scala - Math decrementExact() Method



The Scala Math decrementExact() method returns the argument decreased by one. The method can be overloaded and it can take Int and Long arguments. It throws an exception, if the result overflows an Int or a Long.

Syntax

def decrementExact(x: Int): Int = java.lang.Math.decrementExact(x)
def decrementExact(x: Long): Long = java.lang.Math.decrementExact(x)

Parameters

x Specify the value.

Return Value

Returns the argument decreased by one.

Exception

Throws ArithmeticException, if the result overflows an Int or a Long.

Example:

In the example below, Math.decrementExact() method is used to decrease the argument by one.

object MainObject {
  def main(args: Array[String]) {
    var x1 : Int = 10;
    var x2 : Long = 100;
    println("Math.decrementExact(x1) = "
            + Math.decrementExact(x1)); 
    println("Math.decrementExact(x2) = "
            + Math.decrementExact(x2)); 
  }
}

The output of the above code will be:

Math.decrementExact(x1) = 9
Math.decrementExact(x2) = 99

❮ Scala - Math Methods