# Copyright 2021 - 2025 PeopleWare n.v.
#
# Licensed under the Apache License, Version 2.0 (the “License”);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

type: object
examples:
  - currency: XTS
    decimals: 4
    value: 7475005
  - currency: EUR
    decimals: 2
    value: -84884
  - currency: USD
    decimals: 4
    value: 0
  - currency: KRW
    decimals: 0
    value: 84884
  - currency: XAU
    decimals: -8
    value: 884
description: >-
  Representation of an amount of money, in the given `currency`, with the given `decimals`.


  E.g., `{"currency": "EUR", "decimals": 4, "value": 88584439}` represents €&nbsp;8&nbsp;858,443&nbsp;9.


  The `value` is always expressed as an integer. The decimal it represents is
  <code>value.10<sup>&#8239;‑decimals</sup></code>. E.g., `{"decimals": 4, "value": 88584439}` represents
  858,443&nbsp;9.


  Calculation with decimals **MUST** be exact. Only addition (which implies multiplication with integers) and
  subtraction is safe, but multiplication with non-integers (which implies division) and other operations are not. With
  any other arithmetic operation, the result must explicitly be converted to  definite number of decimals before
  continuing. To what number of decimals this needs to done is an explicit business decision. E.g., to divide 10,00 in 3
  parts, we must decide that we will work with a factor 2, and in some way get 3,33, 3,33, and _3,34_, or,
  alternatively, get 3,33 3 times, and decide what to do with the remaining _0,01_.


  Decimals **MUST** be transported and calculated with as integers, and not as floats or doubles, because some decimal
  fractions, e.g., `0.1`, _cannot be represented as binary 32- or 64-bit floating point numbers_. If we intend 0.1, and
  we represent the value as `0.1` in JSON, it will be interpreted on reception as a number close to, but not equal to
  `0.1`. If we want to calculate 3 times decimal 0.1, and we execute `0.1 + 0.1 + 0.1` or `3 * 0.1`, we get
  `0.30000000000000004 ≠ 0.3`. When we instead represent the decimal as 10∙10<sup>-2</sup>, there is no issue:
  <code>10∙10<sup>-2</sup> + 10∙10<sup>-2</sup> + 10∙10<sup>-2</sup> = (10 + 10 + 10)∙10<sup>-2</sup> =
  30∙10<sup>-2</sup></code>.


  The largest `value` that can be represented is `Number.MAX_SAFE_INTEGER` =
  9&nbsp;007&nbsp;199&nbsp;254&nbsp;740&nbsp;991, and the smallest is `Number.MIN_SAFE_INTEGER` =
  -9&nbsp;007&nbsp;199&nbsp;254&nbsp;740&nbsp;991 (~&nbsp;±9∙10<sup>15</sup>). With 4 decimals, this could represent
  900&nbsp;719&nbsp;925&nbsp;474,0991 ~ 900&nbsp;billion.


  Note that, for addition, all terms must be converted to a representation with the smallest `decimal` value of all
  terms:


  <pre>


  {"decimals": 4, "value": 88584439} +

      {"decimals": 2, "value": 89418456}

  = {"decimals": 2, "value": 885844} +

      {"decimals": 2, "value": 89418456}

  = {"decimals": 2, "value": 90304300}


  </pre>


  This means: 8&nbsp;858,443&nbsp;9 + 894&nbsp;184,56 = 903&nbsp;043,00. The remaining 0,003&nbsp;9 is lost in rounding.


  The advised approach is to never add decimals that have different decimals.
properties:
  decimals:
    type: integer
    examples:
      - 2
      - 0
      - -2
    description: The number of decimal places with which `value` must be interpreted. A positive number means moving the
      decimal point to the left. A negative number means adding zeros at the end.
  value:
    type: integer
    examples:
      - 456568
      - 0
      - -456568
    description: The represented decimal number, multiplied by 10<sup>decimals</sup>. This can be positive, negative, or 0.
      The interpretation of the sign is context dependent.
  currency:
    type: string
    examples:
      - XTS
      - AED
      - AFN
      - ALL
      - AMD
      - ANG
      - AOA
      - ARS
      - AUD
      - AWG
      - AZN
      - BAM
      - BBD
      - BDT
      - BGN
      - BHD
      - BIF
      - BMD
      - BND
      - BOB
      - BOV
      - BRL
      - BSD
      - BTN
      - BWP
      - BYN
      - BZD
      - CAD
      - CDF
      - CHE
      - CHF
      - CHW
      - CLF
      - CLP
      - COP
      - COU
      - CRC
      - CUC
      - CUP
      - CVE
      - CZK
      - DJF
      - DKK
      - DOP
      - DZD
      - EGP
      - ERN
      - ETB
      - EUR
      - FJD
      - FKP
      - GBP
      - GEL
      - GHS
      - GIP
      - GMD
      - GNF
      - GTQ
      - GYD
      - HKD
      - HNL
      - HTG
      - HUF
      - IDR
      - ILS
      - INR
      - IQD
      - IRR
      - ISK
      - JMD
      - JOD
      - JPY
      - KES
      - KGS
      - KHR
      - KMF
      - KPW
      - KRW
      - KWD
      - KYD
      - KZT
      - LAK
      - LBP
      - LKR
      - LRD
      - LSL
      - LYD
      - MAD
      - MDL
      - MGA
      - MKD
      - MMK
      - MNT
      - MOP
      - MRU
      - MUR
      - MVR
      - MWK
      - MXN
      - MXV
      - MYR
      - MZN
      - NAD
      - NGN
      - NIO
      - NOK
      - NPR
      - NZD
      - OMR
      - PAB
      - PEN
      - PGK
      - PHP
      - PKR
      - PLN
      - PYG
      - QAR
      - RON
      - RSD
      - CNY
      - RUB
      - RWF
      - SAR
      - SBD
      - SCR
      - SDG
      - SEK
      - SGD
      - SHP
      - SLE
      - SLL
      - SOS
      - SRD
      - SSP
      - STN
      - SVC
      - SYP
      - SZL
      - THB
      - TJS
      - TMT
      - TND
      - TOP
      - TRY
      - TTD
      - TWD
      - TZS
      - UAH
      - UGX
      - USD
      - USN
      - UYI
      - UYU
      - UYW
      - UZS
      - VED
      - VES
      - VND
      - VUV
      - WST
      - XAF
      - XAG
      - XAU
      - XBA
      - XBB
      - XBC
      - XBD
      - XCD
      - XDR
      - XOF
      - XPD
      - XPF
      - XPT
      - XSU
      - XUA
      - XXX
      - YER
      - ZAR
      - ZMW
      - ZWL
    description: >-
      3-letter [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) alphabetic currency code (3 upper case letters).


      Note that `XTS` is explicitly intended for testing purposes.
    enum:
      - XTS
      - AED
      - AFN
      - ALL
      - AMD
      - ANG
      - AOA
      - ARS
      - AUD
      - AWG
      - AZN
      - BAM
      - BBD
      - BDT
      - BGN
      - BHD
      - BIF
      - BMD
      - BND
      - BOB
      - BOV
      - BRL
      - BSD
      - BTN
      - BWP
      - BYN
      - BZD
      - CAD
      - CDF
      - CHE
      - CHF
      - CHW
      - CLF
      - CLP
      - COP
      - COU
      - CRC
      - CUC
      - CUP
      - CVE
      - CZK
      - DJF
      - DKK
      - DOP
      - DZD
      - EGP
      - ERN
      - ETB
      - EUR
      - FJD
      - FKP
      - GBP
      - GEL
      - GHS
      - GIP
      - GMD
      - GNF
      - GTQ
      - GYD
      - HKD
      - HNL
      - HTG
      - HUF
      - IDR
      - ILS
      - INR
      - IQD
      - IRR
      - ISK
      - JMD
      - JOD
      - JPY
      - KES
      - KGS
      - KHR
      - KMF
      - KPW
      - KRW
      - KWD
      - KYD
      - KZT
      - LAK
      - LBP
      - LKR
      - LRD
      - LSL
      - LYD
      - MAD
      - MDL
      - MGA
      - MKD
      - MMK
      - MNT
      - MOP
      - MRU
      - MUR
      - MVR
      - MWK
      - MXN
      - MXV
      - MYR
      - MZN
      - NAD
      - NGN
      - NIO
      - NOK
      - NPR
      - NZD
      - OMR
      - PAB
      - PEN
      - PGK
      - PHP
      - PKR
      - PLN
      - PYG
      - QAR
      - RON
      - RSD
      - CNY
      - RUB
      - RWF
      - SAR
      - SBD
      - SCR
      - SDG
      - SEK
      - SGD
      - SHP
      - SLE
      - SLL
      - SOS
      - SRD
      - SSP
      - STN
      - SVC
      - SYP
      - SZL
      - THB
      - TJS
      - TMT
      - TND
      - TOP
      - TRY
      - TTD
      - TWD
      - TZS
      - UAH
      - UGX
      - USD
      - USN
      - UYI
      - UYU
      - UYW
      - UZS
      - VED
      - VES
      - VND
      - VUV
      - WST
      - XAF
      - XAG
      - XAU
      - XBA
      - XBB
      - XBC
      - XBD
      - XCD
      - XDR
      - XOF
      - XPD
      - XPF
      - XPT
      - XSU
      - XUA
      - XXX
      - YER
      - ZAR
      - ZMW
      - ZWL
required:
  - decimals
  - value
  - currency
additionalProperties: true
