• psud@aussie.zone
    link
    fedilink
    arrow-up
    41
    arrow-down
    3
    ·
    1 year ago

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

    • janAkali@lemmy.one
      link
      fedilink
      English
      arrow-up
      20
      ·
      edit-2
      1 year ago

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

      • Slotos@feddit.nl
        link
        fedilink
        arrow-up
        1
        ·
        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.

    • Jajcus@kbin.social
      link
      fedilink
      arrow-up
      18
      ·
      1 year ago

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

      • CanadaPlus@lemmy.sdf.org
        link
        fedilink
        arrow-up
        12
        ·
        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.

    • Buckshot@programming.dev
      link
      fedilink
      arrow-up
      9
      ·
      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.

    • redcalcium@lemmy.institute
      link
      fedilink
      arrow-up
      2
      ·
      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.