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

154
Visualizações
How to replace NULL% by 0%

In my calculatePercents () method, I should get 0% instead of NULL%.

enter image description here

In my console.log, I get prints with the values NULL

enter image description here

 calculatePercents() {
    this.v.global.total.amount = this.v.global.cash.amount + this.v.global.sec.amount;
    console.log("Cash => " + JSON.stringify(this.v.global.total.amount));
    console.log('------------------------');
    this.v.global.cash.percent = (this.v.global.cash.amount / (this.v.global.cash.amount + this.v.global.sec.amount)) * 100;
    console.log(" Global cash " + JSON.stringify(this.v.global.cash.percent));

    this.v.global.sec.percent = (this.v.global.sec.amount / (this.v.global.cash.amount + this.v.global.sec.amount)) * 100;
    console.log(" NULL ???? ==> " + JSON.stringify(this.v.global.sec.percent));

    var totPercent = (this.v.global.cash.percent + this.v.global.sec.percent);

    this.v.global.total.percent = totPercent;

}

How I could get a 0 instead of NULL please.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can try this in your method;

if(this.v.global.cash.amount == null){
         this.v.global.cash.amount = 0
    }
if(this.v.global.sec.amount == null){
     
         this.v.global.sec.amount = 0
    }

And then you can do that; (your method)

calculatePercents() {
this.v.global.total.amount = this.v.global.cash.amount + this.v.global.sec.amount;
console.log("Cash => " + JSON.stringify(this.v.global.total.amount));
console.log('------------------------');
this.v.global.cash.percent = (this.v.global.cash.amount / (this.v.global.cash.amount + this.v.global.sec.amount)) * 100;
console.log(" Global cash " + JSON.stringify(this.v.global.cash.percent));

this.v.global.sec.percent = (this.v.global.sec.amount / (this.v.global.cash.amount + this.v.global.sec.amount)) * 100;
console.log(" NULL ???? ==> " + JSON.stringify(this.v.global.sec.percent));

var totPercent = (this.v.global.cash.percent + this.v.global.sec.percent);

this.v.global.total.percent = totPercent;

}

And your veriable type is can be "let". Simple Code:

let a = null;

if(a==null){
   a=0
}
console.log(a);
// output: 0
about 4 years ago · Juan Pablo Isaza Relatório

0

with || if the first value is false (converted as boolean) then it will take the second, in that case it's used as a default value.

this.v.global.total.percent = totPercent || 0;
about 4 years ago · Juan Pablo Isaza Relatório

0

you can use || operator to set zero

Eg:

this.v.global.total.percent = (totPercent||0);

for more https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR

about 4 years ago · Juan Pablo Isaza 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