this post was submitted on 17 Sep 2023
284 points (100.0% liked)

Programmer Humor

420 readers
22 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] psud@aussie.zone 32 points 1 year ago (9 children)

If working in currency, work in cents and divide by 100 and round to 2 decimals for output

[–] IWantToFuckSpez@kbin.social 38 points 1 year ago (1 children)

Yeah round down and transfer the remainder to an account you own.

[–] psud@aussie.zone 6 points 1 year ago
[–] janAkali@lemmy.one 13 points 1 year ago* (last edited 1 year ago) (1 children)

Most languages have decimal libraries to correctly handle floating point arithmetics, where precision is necessary.

[–] Slotos@feddit.nl 1 points 1 year ago (1 children)

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 points 1 year ago

Also decimal system is not exatcly that much better since you also cant write 1/3 in decimal

[–] Jajcus@kbin.social 9 points 1 year ago (1 children)

If working with currency use types and formating functions appropriate for currency. Not float.

[–] psud@aussie.zone 2 points 1 year ago* (last edited 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.

I am aware my statement isn't true in several languages

[–] Buckshot@programming.dev 7 points 1 year ago (1 children)

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 points 1 year ago

Buckshot@programming.dev wake up, new currencies just dropped

[–] Zikeji@programming.dev 2 points 1 year ago

That's what I wound up doing on a work project. Works really well.

[–] redcalcium@lemmy.institute 2 points 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.

load more comments (3 replies)