• Scrubbles@poptalk.scrubbles.tech
    link
    fedilink
    English
    arrow-up
    1
    ·
    7 hours ago

    But the second you go international that goes out the window. There are currencies with 3 and 4 digits of precision. There are currencies that are integers. Keeping track of that is a nightmare using a numerical value. It’s safest just to represent it as a string.

      • Scrubbles@poptalk.scrubbles.tech
        link
        fedilink
        English
        arrow-up
        1
        ·
        44 minutes ago

        Right but then you have to pass that information around so people know how to deserialize it, and it means things like the UI need to do exact currency conversions on their side that must match the server too. So if you are doing USD you would not only need to pass 1000 to denote $10 but also the currency name, USD, and it’s precision value of 2. However if you are using the Dinar, and they pass the same 1000 they would need to make sure they pass the precision of 3, and the UI would need to calculate that. (And remember JS is floats, so you run the risk there that the value may not be the same as what the server would see)

        The best course of action is to format it on the server. I’ve found that passing the currency code is good along with the stringified value of “10.00” or in the Dinar case “1.000”. That way the UI knows exactly what it should show. Its also extremely rare that someone needs to modify a value on the frontend, but if they do most currency libraries prefer string anyway.

        Source: I’ve done FinTech for trading companies, banks, and payment processors. There are a lot of gotchas with money