Most languages have decimal libraries to avoid working with floating point numbers.
Comment on 0.30000000000000004
psud@aussie.zone 1 year ago
If working in currency, work in cents and divide by 100 and round to 2 decimals for output
janAkali@lemmy.one 1 year ago
Slotos@feddit.nl 1 year ago
They are as incapable of handling one third of a dollar as binary positional notation is incapable of handling one fifth (0.2).
It’s not really a float problem. It’s a positional notation one. Some perfectly rational numbers refuse to squeeze into that mold.
szczuroarturo@programming.dev 1 year ago
Also decimal system is not exatcly that much better since you also cant write 1/3 in decimal
Jajcus@kbin.social 1 year ago
If working with currency use types and formating functions appropriate for currency. Not float.
psud@aussie.zone 1 year ago
I was recalling a project in perl, which doesn’t have a variety of types. If you add values, you get a scalar, which will be a float if the numbers are not integers.
Buckshot@programming.dev 1 year ago
And remember not all currencies are 2dp so get a list and use the appropriate exponent.
I had to update our currency database this week because there’s new currencies. It’s almost as bad as timezones.
ruk_n_rul@monyet.cc 1 year ago
Buckshot@programming.dev wake up, new currencies just dropped
MJBrune@beehaw.org 1 year ago
Even then that can contain bugs on large amounts.
CanadaPlus@lemmy.sdf.org 1 year ago
You’re telling me there’s someone that has more than 20 million dollars? /s
If you’re handling people’s money you should probably be using arbitrary-precision arithmetic. I mean, you might get away with a long int, but finance is serious business and the amount of data you’re going to be processing relative to your funding is probably going to be small.
psud@aussie.zone 1 year ago
Not the project I was thinking about above, but at work my team delivered software handling 13 digit numbers, but that’s in COBOL which does fine with money
Zikeji@programming.dev 1 year ago
That’s what I wound up doing on a work project. Works really well.
redcalcium@lemmy.institute 1 year ago
Some programming languages use different rounding method. Might bite you in the ass if you’re not aware of it and using multiple programming language in your application to handle different areas.
IWantToFuckSpez@kbin.social 1 year ago
Yeah round down and transfer the remainder to an account you own.
psud@aussie.zone 1 year ago
Indeed, Lex
elbarto777@lemmy.world 1 year ago
No, not Lex. Gus!
psud@aussie.zone 1 year ago
I just recalled, in that project I did have to divide money, which would leave fractional cents, so I divided as integers with remainder, and assigned the remainder to one side of the division