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

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

  •  22-07-2023
  •  | 
  •  

문제

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
    }
}
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top