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?