• FizzyOrange@programming.dev
    link
    fedilink
    arrow-up
    1
    arrow-down
    9
    ·
    1 month ago

    Honestly none of those “gotchas” sound at all surprising to me. A currency enum and f64 for the amount will cover 99.99999% of use cases. u64.u64 fixed point if you really need it (you probably don’t).

    Try dates/times. That’s serious pain.

    • Scrubbles@poptalk.scrubbles.tech
      link
      fedilink
      English
      arrow-up
      17
      ·
      edit-2
      1 month ago

      Wow uh, nope. A f64 is a double, floating point precision type, aka not precise. You should never store currency values in that, otherwise you, well you just recreated the exact bug we see in the image above.

      Bud, like I said, the problem comes off as “lol it’s so easy”, but unless you have literally worked with money before - real actual dollars and cents, it is much more complex than you give it credit for.

      Your approach may appear fine, it may look fine, but with money precision matters. Floating points are not precise, they are an exponential representation of a function that will give you a pretty good estimation of the value you want - but they are not precise. In money, you must be precise. You fuck up that value, you get customers calling in, you get banks rejecting your transfers, you get governments closing you down because your code is shit and they don’t trust you to manage people’s money.

      Now, I’m not a jerk, I’ll tell you the correct answer, we should all learn. The correct format to store money in? A string. Immutable, perfectly precise to whatever precision you want, and whatever the customer types in is exactly what you can store. You parse that string to make sure it’s valid, you use your currency enum as you said (which I wouldn’t btw, currencies actually change more often than you would expect), to make sure it passes to a correct numerical value that fits within the bounds of that currency. If you need to mutate that value, in the same language you go to a precise type, mutate, and then return it back as a string value. Storing in a database then you store the value as string, and best practice is to store the “imprecise” amount next to it as whatever the database supports. That way, you have the auditable real value ($4.00) stored for use on reports and taxes, and the imprecise ($3.99999999898998888181) that you can use for sorting, filtering, and regular database options.

      Strings also should be used over the wire, aka in json payloads in rest apis. This is because JavaScript and other languages parse the number types as floats, which again are imprecise. String is the only safe way to translate money between languages and data stores.

      For Rust, as I assume that’s what you’re referring to, you should use something like rust_decimal - crates.io: Rust Package Registry https://crates.io/crates/rust_decimal a precision based crate for handling money.

        • GissaMittJobb@lemmy.ml
          link
          fedilink
          arrow-up
          2
          ·
          1 month ago

          You can’t use it if you ever want to do any operations on said money, due to the loss of prescision.

          Enums are not a good idea for the currencies either.

          • FizzyOrange@programming.dev
            link
            fedilink
            arrow-up
            1
            arrow-down
            1
            ·
            edit-2
            1 month ago

            Yes you can. You’re vastly underestimating the size of an f64. Give me a concrete example of a money operation that fails with f64 (for normal companies; assuming you aren’t a stock exchange or Visa or whatever).

            Enums are not a good idea for the currencies either.

            Why not?

            • GissaMittJobb@lemmy.ml
              link
              fedilink
              arrow-up
              3
              ·
              1 month ago

              Yes you can. You’re vastly underestimating the size of an f64. Give me a concrete example of a money operation that fails with f64 (for normal companies; assuming you aren’t a stock exchange or Visa or whatever).

              0.1f64 + 0.2f64 != 0.3f64

              Why not?

              Encoding in assumptions about a fixed amount of supported currencies in a system is broadly speaking not a good idea

              • FizzyOrange@programming.dev
                link
                fedilink
                arrow-up
                1
                arrow-down
                1
                ·
                1 month ago

                0.1f64 + 0.2f64 != 0.3f64

                It does if you round it to the nearest penny.

                Encoding in assumptions about a fixed amount of supported currencies in a system is broadly speaking not a good idea

                Most sensible programming languages allow enums to be non-exhaustive.