• hperrin
    link
    fedilink
    English
    arrow-up
    1
    ·
    6 hours ago

    If you’re passing a string, and you don’t know what currency it is, you have the exact same problem as passing an int and not knowing what currency it is. USD’s smallest denomination is 1/10 cent (gas stations usually charge in tenths of a cent, and half pennies are still legal tender, even though they’re not minted anymore), so the string representation in your examples would be exactly the same for USD and Dinar.

    I agree that the best way to represent it between server and client is as a string, but that doesn’t work when you need to perform calculations, so in that case, the best way to do the calculations is to use the smallest denomination, then use banker’s rounding on the result, then use the int for storage and turn it back into a string representing the default denomination for transit. Or, just use ints representing the smallest denomination everywhere except displaying to the user. Even JavaScript can handle integer arithmetic.

    Source: I’ve also done fintech for loan and retail companies. Yes, there are definitely a lot of gotchas, so using integers is best when you need to do calculations.