News for hackers. Stuff that matters.
Re:Your balance is $0.30000000004 by kennu on Tuesday February 18, 2020 @11:22AM (#22357115)
This approach doesn't work when dealing with fractional cents, like charging $0.0001 per hour for something.

I would rather recommend using an arbitrary precision decimal library like Decimal.js, and using strings to represent money in JSON. And of course using an appropriate database type for storing the data.

Re:Your balance is $0.30000000004 by beerandt on Tuesday February 18, 2020 @11:34AM (#22357220)
Take the inverse and use fractional hours per penny.

Edit: And sum your (optionally price-weighted) time first.

Re:Your balance is $0.30000000004 by lanna on Tuesday February 18, 2020 @11:25AM (#22357150)
Just make the smallest possible fractional cent the unit then
Re:Your balance is $0.30000000004 by kennu on Tuesday February 18, 2020 @12:36PM (#22357997)
That works if you are 100% sure you know beforehand what the smallest possible fraction will be.

What you gain by consistently using an arbitrary precision decimal library to process all monetary values, is the freedom to process smaller fractions later if necessary. Also you'll never overflow with really big values.

In other words, when you represent monetary values with arbitrary precision Decimal objects everywhere, you can easily expand your application as much as you want on either side of the decimal dot. Changing existing integer-based code do to that is very error-prone, you quickly lose sight of what exactly an integer value or parameter means in different places.

Re:Your balance is $0.30000000004 by Kiro on Tuesday February 18, 2020 @12:25PM (#22357819)
What if you can't know that beforehand? E.g. if you suddenly need to charge $0.000000001 per second.