Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
How does Javascript's number precision affect large values?

So I have a project where I am dealing with both really small numbers and really large numbers. Essentially I have a counter that is counting up from 10^-9 or so. Eventually, it reaches 1 and continues counting upwards by a really small factor, but very quickly.

This counter represents a "currency" in my project. So when the user presses a button, if the "cost" associated with that button is lower than the currency, the cost is subtracted from the user's currency and they are given that item. Here's what I mean:

if (currency >= itemCost) {
        items++
        currency = currency - itemCost
        itemCost = itemCost * 2

So for example, at the beginning of the game, an example value of currency and itemCost would be 0.000000003020 or so. When a cost is subtracted at this level, the program works as intended. However, when I pass 1, it ~SOMETIMES~, stops working and sometimes it doesn't. Although it will sometimes keep working until it breaks at some arbitrary quantity. At this point, it will also call the above function if itemCost is more than the currency (at least according to their corresponding labels), leaving the user with a negative currency.

I am wondering if this is happening because the number is too long to represent both the digits greater than 1 and the long string of decimal digits, like 2347.23482348982483 for example. Is there a limit to how many digits I can accurately represent in Javascript? Would I be okay with rounding the number to say 6 decimal digits after passing 1, or do I need to look into modules that help me with the precision that I need?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda