Money as an object with a string amount; UN/ECE Rec 20 units
Status
Section titled “Status”Accepted.
2026-11-03 (owner rulings; the contract was added on 2026-11-04).
Context
Section titled “Context”Logistics and finance code needs exact values: prices to the cent, weights to the gram, conversions that agree between two languages to the last digit. Floating point does not give that, and a JSON number can pass through a float on the way in any consumer. Java has BigDecimal; Go has no standard decimal type. A value also needs its currency or unit, or it is ambiguous, and units have to match what partner systems and e-invoicing exchange.
Several choices were open: how an amount appears on the wire, how a unit is named, whether Go should use a library decimal, and how much of packaging and temperature to include.
Decision
Section titled “Decision”- Money travels as an object with a string amount:
{"amount": "3132.48", "currency": "ZAR"}. A quantity is{"value": "7831.2", "unit": "KGM"}. Amounts and values are always JSON strings, never numbers, and keep their scale. Reading is strict: every member required, no others, and a JSON number is refused. - Units travel as UN/ECE Recommendation 20 codes (
KGM,MTR,LTR), case-sensitive. A symbol such askgis for display. Parsing accepts documented aliases, and an application can register its own on a unit registry. The unit table is a data file both languages embed. - Currencies are ISO 4217 codes with their minor units, from one generated table.
- Precision is kept. Operations never round unless asked. Division requires an explicit scale and rounding mode. Allocation splits an amount by ratios so the parts always sum exactly to the rounded whole.
- Conversion factors are exact legal definitions, held as fractions (1 lb is 0.45359237 kg), not rounded floats. Temperature uses an offset.
- Go has its own decimal on
math/big, standard library only. A third-party decimal library divides to a package-wide precision unless told otherwise, which is the silent inexact division the contract forbids, and its string output drops trailing zeros thatBigDecimalkeeps. - Locale formatting is not here. These types produce plain text; the UI kits format.
- In the database a value is two columns, for example
<field>_amount NUMERICand<field>_currency CHAR(3).
Consequences
Section titled “Consequences”Good:
- No float ever touches an amount, on the wire or in storage.
- Java and Go agree exactly, which the measure conformance vectors prove.
- Unit codes match the data partners already exchange.
Bad:
- A string amount is clumsier to read and needs a parser on the consumer side.
- Maintaining our own decimal type is a cost, offset by owning its semantics.
- Codes such as
KGMare less friendly thankgin a payload a person reads.
Alternatives considered
Section titled “Alternatives considered”| Alternative | Why not |
|---|---|
| JSON numbers | Pass through floats and lose scale. |
A single string such as "3132.48 ZAR" |
Needs custom parsing and does not fit schema validation. |
| Symbols on the wire | Ambiguous across locales and not what EDI exchanges. |
| A library decimal for Go | Global precision and trailing-zero behaviour differ from the reference. |