menu
announcement

Spectrum is now read-only. Learn more about the decision in our official announcement.

Node.js

A Spectrum community for Node.js ✨🐢🚀✨

Channels
Team

Accounting with node.js

July 4, 2019 at 11:47am

Accounting with node.js

July 4, 2019 at 11:47am
I wana build an accounting app with Node.js but not sure is it suitable. or is there any docker container that support my project for better calculation. because 0.1 + 0.2 === 0.33333333334 in javascript. any Advice

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.
like-fill
1
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_INTEGER you're unlikely to hit those limits in regular accounting calculations).
For this reason JS now has a BigInt type that only supports integer values (for a BigInt numeric literal you can just suffix numbers with an n, so 23n is a BigInt whereas 23 is 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!
Edited