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

108
Visualizações
From string to integer Javascript
function FinalAmount() {
  var FinalPrice = document.getElementById("FinalPrice");
  let AllProductTotalPrice = 0;

  for (let index = 0; index < OrderedProductList.length; ++index) {
    alert;
    var CurrentProductTotal = 0;
    CurrentProductTotal = OrderedProductList[index].TotalPrice;
    console.log(CurrentProductTotal);
    AllProductTotalPrice += CurrentProductTotal;
    console.log(AllProductTotalPrice);
  }
  FinalAllProductPrice = AllProductTotalPrice;
  FinalPrice.innerText = "RM" + FinalAllProductPrice;
}

This is my javascript code. I would have a question about why the console.log(CurrentProductTotal) is already an integer but the above AllProductTotalPrice is a string value. Is there any way to let its sum as an integer and bring it to the below FinalAllProductPrice. Please help me TT

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

0

I think the easiest way would be to

use Number() to parse a string to a number. (we don't define integers specifically in javascript)

const num = "1234"
Number(num)

⚠️ If you give it a string which does not solely consist out of numbers it will return NaN

So in your example that would be:

CurrentProductTotal = OrderedProductList[index].TotalPrice

Now to your questions:

  • console.log() automatically displays it formatted for you. I would use the typeof operator to check whether CurrentProductTotal is really a number, or a string which the console formats differently which is a bit confusing.

Try this in your browser console:


const num = "1234"
> undefined
num
> '1234'
console.log(num)
> 1234 // formatted like a number
console.log(num, typeof num)
> 1234 string // still formatted like a number, but a string in reality

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