# 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: EUR
    decimals: 2
    value: 0
  - currency: EUR
    decimals: 2
    value: 0
  - currency: EUR
    decimals: 2
    value: -9007199254740991
  - currency: EUR
    decimals: 2
    value: 9007199254740991
description: >-
  The value has 2 decimals, and must be in the interval [-90 071 992 547 409,91; 90 071 992 547 409,91]


  The currency is EUR.


  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:
  currency:
    examples:
      - EUR
    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.


      In this case, this is fixed to EUR.
    const: EUR
  decimals:
    examples:
      - 2
    description: The number of decimal places with which `value` must be interpreted. Must equal 2.
    const: 2
  value:
    type: integer
    examples:
      - 0
      - 0
      - -9007199254740991
      - 9007199254740991
    description: The represented decimal number, multiplied by 10<sup>decimals</sup>. Must be in the interval [undefined;
      undefined],
    minimum: -9007199254740991
    maximum: 9007199254740991
required:
  - currency
  - decimals
  - value
additionalProperties: true
