Why aren't currency values returned by the Square Connect API formatted with decimals?

StackOverflow https://stackoverflow.com/questions/23645395

  •  22-07-2023
  •  | 
  •  

Question

Here's a snippet of data returned by the API. The amount tendered in the transaction was actually $1.00 USD, but the API returns a value of 100. Is this normal? Should I expect to have to add the decimal and decimal places, myself?

{
    "type": "CASH",
    "name": "Cash",
    "total_money": {
        "currency_code": "USD",
        "amount": 100
    },
    "tendered_money": {
        "currency_code": "USD",
        "amount": 100
    }
}
Était-ce utile?

La solution

The problem is that lots of languages, Javascript included, don't have decimals. They have floats instead. And you should never do monetary calculations (that you care about) with floats due to the floating point problem:

As such, returning the rates in cents is a good way to remind programmers of this and avoid the temptation of doing financial calculations in floats.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top