Accounting with node.jsJuly 4, 2019 at 11:47am
July 4, 2019 at 11:57am
Hey man, this is my issue before, to solve this I use integer to store the currency and use something like dinero to parse and display it. I can say it works pretty well. Actually you can see some other solutions as well online on how they solve the issue about precision.
When dealing with money it's also common practice to calculate with cents, so you only handle integers. That approach is not just common for JS, but also for other programming languages. Depends a bit on your use case, though. Maybe you do need to deal with fractions of cents for some weird edge cases, but usually it should be fine.
July 8, 2019 at 12:15pm
Keep in mind that JS only has floats, not integers, although JS engines may decide to store certain values as integers internally. This means even sticking to integers is unsafe at scale (though if you look at
Number.MAX_SAFE_INTEGERyou're unlikely to hit those limits in regular accounting calculations).
For this reason JS now has a
BigInttype that only supports integer values (for a BigInt numeric literal you can just suffix numbers with an
23nis a BigInt whereas
23is a Number). This works in modern versions of Node and most recent browsers.
Practically speaking I wouldn't worry about this too much and go with a library like dinero as Joshua has suggested though. Dinero is purpose built for currency calculations and formatting and it's actively maintained while also being fairly mature.
July 28, 2019 at 10:02am
June 28, 2021 at 2:48pm
Lol, don't worry about the 0.1+0.2=0.33333333334 thing. It's something very common in programming. If I'm not wrong, it's like this in most programming languages. That's not a mistake; that's just math. You'll have to dig deeper to understand what is happening behind the scenes. First, let me ask you whether you know how numbers are represented for computational purposes? If not, google it and give it a read. Regarding the accounting app, I would recommend seeking the help of an expert from https://www.ldgaccountingservices.com. They're super professional, and they might be able to help. Good luck!