Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.3K
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda