• @psud@aussie.zone
    link
    fedilink
    4110 months ago

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

        • El Barto
          link
          fedilink
          9
          edit-2
          10 months ago

          No, not Lex. Gus!

          Awesome movie, regardless!

          • @psud@aussie.zone
            link
            fedilink
            3
            edit-2
            10 months ago

            I just recalled, in that project I did have to divide money, which would leave fractional cents

            It was a budgeting program, I could put rogue cents where I liked. I think my solution made accounts due $12.553333333… (internally 1255.3333…) each pay period get 12.54, so after n/3 pay periods they’d be 2n cents over. I could deal with that imprecision.

    • janAkali
      link
      English
      20
      edit-2
      10 months ago

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

      • @Slotos@feddit.nl
        link
        fedilink
        110 months 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
      link
      fedilink
      1810 months ago

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

      • @psud@aussie.zone
        link
        fedilink
        4
        edit-2
        10 months 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

      • @CanadaPlus@lemmy.sdf.org
        link
        fedilink
        1210 months 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
          link
          fedilink
          110 months 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

    • @Buckshot@programming.dev
      link
      fedilink
      910 months 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
      210 months 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.