Scala - Math toIntExact() Method
The Scala Math toIntExact() method returns the value of the Long argument. The method throws an exception if the value of the argument overflows an Int.
Syntax
def toIntExact(x: Long): Int = java.lang.Math.toIntExact(x)
Parameters
x |
Specify the long value. |
Return Value
Returns the value of the Long argument. Throws an exception if the value overflows an Int.
Exception
Throws ArithmeticException, if the argument overflows an Int.
Example:
In the example below, Math.toIntExact() method returns the value of the Long argument.
object MainObject { def main(args: Array[String]) { var x : Long = 100; var y : Long = Int.MaxValue; println("Math.toIntExact(x) = " + Math.toIntExact(x)); println("Math.toIntExact(y) = " + Math.toIntExact(y)); } }
The output of the above code will be:
Math.toIntExact(x) = 100 Math.toIntExact(y) = 2147483647
❮ Scala - Math Methods