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

135
Visualizações
How to handle big numbers in java script

I am trying to make a cookie clicker type game for fun and I ran into a problem. As you get into bigger numbers in a lot of games, cookie clicker for example, the counter doesn't just have a 20 digit number up there, instead it takes the multiple of a thousand i.e.(million, billion, trillion) and so on. I was wondering how I would make this possible so that it can display a drastically high number while still being user friendly and still have the possible math operations applied to it. An important note is that when it passes 1000 of the certain suffix then it rolls over to the next, on the other hand if it goes underneath 1 then it rolls back a multiple of 1000. If anyone has an idea it would be really helpful.

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

0

Here's an example using regular javascript numbers. This outputs a number less than a thousand plus a symbol

const map = ["", "K", "M", "B", "T", "Quad", "Quint", "Sext", "Sept", "Oct", "Nov", "Dec"];

const outputAsCookies = (number) => {
  if (number < 1000) return number.toString();

  let log = Math.log10(number);
  let div = log - log % 3;
  let index = div / 3;
  while (index >= map.length) {
    // ran out of map elements
    index -= 1;
    div -= 3;
  }
  
  return (number / Math.pow(10, div)).toPrecision(6) + " " + map[index];
};
console.log(outputAsCookies(100));
console.log(outputAsCookies(1000));
console.log(outputAsCookies(98765432109876));
console.log(outputAsCookies(3.1415926535e32));

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