Mosius
Mosius

Reputation: 1682

Kotlin: Unexpected value for 2 Int multiplication

I've written a function to round a value in billion, here is my code:

private fun roundBillion(value: Double): Int {
    val a = (value / 1000000).toInt()
    val res = a * 1000000
    return res
}

but when I execute the function I get an unexpected value in res variable. here is variables inspection when the break point is on return statement:

value = 1.7636265135946954E11

a = 176362

res = 268340864

I can't figure out where the problem is!

Screen Shot

Upvotes: 0

Views: 1132

Answers (1)

Roland
Roland

Reputation: 23312

What you are experiencing is an integer overflow.

Double.MAX_VALUE is 1.7976931348623157E308.

Int.MAX_VALUE is 2147483647. Your number in the calculation (i.e. 176362000000) exceeds that.

Upvotes: 4

Related Questions