Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type representing money values #105

Closed
rartych opened this issue Oct 2, 2023 · 3 comments
Closed

Type representing money values #105

rartych opened this issue Oct 2, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@rartych
Copy link
Collaborator

rartych commented Oct 2, 2023

Problem description
Currently amounts of money are defined as:

        amount:
          type: number
          format: float
          multipleOf: 0.01

Swafgger docs indicates

multipleOf may be used with floating-point numbers, but in practice this can be unreliable due to the limited precision or floating point math.

Possible evolution

Storing amount values as long integers is a common best practice

Field Name OpenAPI type OpenAPI format Description
amount integer int64 Represented in "minor units"
currency string currency "Minor units" depend on the currency value.

Note: Minor units are specified according to ISO 4217 and can be found in this table.

Additional context

Recommendation in:
https://github.com/team-monite/api-style-guide/blob/main/Guidelines.md#should-use-the-common-money-object

Storing amount values as long integers is a common best practice, adopted by API payment providers like Adyen and Stripe.

Most currencies have two decimals. Some currencies do not have decimals, and some have three decimals. For example:

  • GBP 10: GBP has two decimals, so in minor units submit an amount of 1000
  • JPY 10: JPY has no decimals, so in minor units submit an amount of 10
  • BHD 10: BHD has three decimals, so in minor units submit an amount of 10000
@rartych rartych added the enhancement New feature or request label Oct 2, 2023
@PedroDiez
Copy link
Collaborator

PedroDiez commented Oct 25, 2023

Have checked this internally. In Telefonica, billing/charging systems manage "decimals" in a natural way.

Providing amount as "integer" would have huge impacts in systems as it is needed to manage a conversion to identify the number of decimals to be considered. Usually merchants are aware of this kind of stuff in different countries and manage it.

Then our position is to keep current model. Would also like to know opinions from other participants.

Anyway, an enhancement is derived from your analysis @rartych.
We need to address scenarios with 3 decimals.

So for amount, should be needed to be described as:

        amount:
          type: number
          format: float
          multipleOf: 0.001

float also includes integers so for currencies that do not consider decimals an integer can be signalled and Schema validation does cover it.

@PedroDiez
Copy link
Collaborator

25/10: Moving forward with

        amount:
          type: number
          format: float
          multipleOf: 0.001

@PedroDiez
Copy link
Collaborator

PedroDiez commented Nov 16, 2023

Closed as per PR#119 MERGED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants