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

286
Visualizações
Why does my Vanilla JS Calculator's number change at the 17th digit?

It's a basic calculator made by the youtube channel Web Dev Simplified. He did a youtube tutorial on it, which I followed along and basically made the same thing.

Both his example calculator and mine has the same problem, where if I pressed on any of the number buttons (for example '1') 17 or more times, the numbers entered after the 16th digit displays as a different number for some reason (17th digit becomes '2' and not '1').

It does this with any number entered. I don't understand why.

I attached a codepen link so that you can see both his code and the code output.

You will find the display output in this part of the HTML code:

  <div class="output">
  <div data-previous-operand class="previous-operand"></div>
  <div data-current-operand class="current-operand"></div>
  </div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

it is because your number is over than integer add console.log() in the getDisplayNumber function and Compare the log and the display data

in the log, it looks like 1.1111111111111111e+21 when out of the range

What is JavaScript's highest integer value that a number can go to without losing precision?

getDisplayNumber(number) {
const stringNumber = number.toString()
const integerDigits = parseFloat(stringNumber.split('.')[0])
const decimalDigits = stringNumber.split('.')[1]
console.log(integerDigits)
let integerDisplay
if (isNaN(integerDigits)) {
  integerDisplay = ''
} else {
  integerDisplay = integerDigits.toLocaleString('en', { maximumFractionDigits: 0 })
}
if (decimalDigits != null) {
  return `${integerDisplay}.${decimalDigits}`
} else {
  return integerDisplay
}   }
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