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

519
Visualizações
How to Check Double value is Null or Zero in kotlin

How to Check Double value is Null or Zero in kotlin

val ratio:Double? = 0.0

val calRatio = if (ratio == null || ratio == 0.0)
        0.12
    else
        ratio

ratio in null , 0.0 , 0.1

if ratio null or 0.0 then return 0.12

and ratio is 0.2 or more then return same ratio value

how to check this algorithm not use if statement

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can write this in idomatic Kotlin without an if statement using takeUnless.

val ratio: Double? = 0.0
val calRatio = ratio.takeUnless { it == 0.0 } ?: 0.12

The takeUnless call checks whether the number matches the predicate it == 0.0. If the predicate evaluates to true, null is returned. Only when the predicate evaluates to false is the actual number returned.

We can see why this works by considering the three possible cases:

  1. When ratio is null, the predicate it == 0.0 evaluates to false. The call to ratio.takeUnless { it == 0.0 } returns the value of ratio, which is null. Because its left-hand-side operand is null, the ?: operator returns the right-hand-side value of 0.12.
  2. When ratio is 0.0, the predicate it == 0.0 evaluates to true. The call to ratio.takeUnless { it == 0.0 } ignores the value of ratio and instead returns null. Because its left-hand-side operand is null, the ?: operator returns the right-hand-side value of 0.12.
  3. When ratio is any non-null, non-zero number, the predicate it == 0.0 evaluates to false. The call to ratio.takeUnless { it == 0.0 } returns the value of ratio, which is the original number. Because its left-hand-side operand is not null, the ?: operator returns the left-hand-side value.
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