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

1.3K
Views
how to divide two integer into decimal in kotlin?

i divide two Integers (e.x 3/6) and i'm trying to get a result of 0.500000 in kotlin. i've tried some solutions but none of them solve my problem like.

val num = BigDecimal(3.div(6))

    println("%.6f".format(num))

   

but the result is 0.000000

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

3 and 6 are both Int, and dividing one Int by another gives an Int: that's why you get back 0. To get a non-integer value you need to get the result of the division to be a non-integer value. One way to do this is convert the Int to something else before dividing it, e.g.:

val num = 3.toDouble() / 6

num will now be a Double with a value of 0.5, which you can format as a string as you wish.

over 4 years ago · Santiago Trujillo Report

0

You might have better luck with:

val num = 3.toBigDecimal().divide(6.toBigDecimal())
println(num)
// prints 0.5

You have to convert both numbers to BigDecimal for the method to work. This will show the exact quotient, or throw an exception if the exact quotient cannot be represented (ie a non-terminating decimal).

You can set the scale and rounding mode as follows:

val num = 3.toBigDecimal().divide(6.toBigDecimal(), 4, RoundingMode.HALF_UP)
println(num)
// prints 0.5000

Link to reference article

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!