Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Long to Int in Kotlin?

Tags:

kotlin

I want to do something like this:

fun process(minutes: Int) = 0  fun test() {     process(System.currentTimeMillis() / 1000 / 60) // error: Int expected } 

and if I try process((System.currentTimeMillis() / 1000 / 60) as Int) I get a ClassCastException at runtime.

So how can I convert Long to Int?

like image 877
netimen Avatar asked Mar 10 '16 10:03

netimen


People also ask

How do I cast a datatype in Kotlin?

In Smart Casting, we generally use is or !is an operator to check the type of variable, and the compiler automatically casts the variable to the target type, but in explicit type casting we use as operator. Explicit type casting can be done using : Unsafe cast operator: as.


Video Answer


1 Answers

Use Long.toInt():

process((System.currentTimeMillis() / 1000 / 60).toInt())  
like image 184
Aleksander Blomskøld Avatar answered Oct 04 '22 13:10

Aleksander Blomskøld