Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.3K
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda