Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

481
Views
Kotlin: ClassCastException when casting double to integer?

I need to do a for loop in Kotlin:

for (setNum in 1..(savedExercisesMap[exerciseKey] as HashMap<*, *>)["sets"] as Int){

But I get this error:

java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer

I wouldn't think this would be an issue. Is there a reason why this is happening and how to fix?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Casting from Double to Int will never succeed using as keyword. They both extend Number class and neither extends the other, so this cast is neither downcasting or upcasting. To convert a double to int in Kotlin you should use .toInt() function.

val aDouble: Double = 2.22222
//val anInt = aDouble as Int // wrong
val anInt = aDouble.toInt() // correct
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!