Sum decimal amount strings with an explicit scale and no rounding
Python
Aggregate amounts exactly and reject silent precision loss.
Aggregate amounts exactly and reject silent precision loss. This stdin-based Python CLI processes one bounded JSON request and returns the documented JSON result. It does not execute jobs or contact external services. Inspect its limits before adapting it to an application.
Before you start
Python 3.12 standard library; no third-party packages. Verified with the recorded pinned runtime.
One UTF-8 JSON object on stdin, no command-line arguments. Maximum encoded request size is 20000 bytes.
Build and check
Save the source as sum-decimal-amounts-python.py.
Save the example JSON below as request.json.
Run python3 sum-decimal-amounts-python.py < request.json.
Compare the parsed JSON result with the expected output. Invalid input exits 2 with empty stdout and a generic stderr message.
Limits to understand
Input is limited to 20000 bytes, 500 amounts, and an integer scale from 0 through 6.
Amounts must follow the specified decimal string grammar; fractional digits exceeding the scale are rejected, including trailing zeros. No rounding is performed.
This does not convert currencies, apply exchange rates, calculate taxes, or implement any financial policy.
Input exactly {"amounts":array of at most 500 strings,"scale":integer 0..6}; booleans forbidden.
Amount grammar: optional minus, then 0 or a nonzero ASCII digit followed by up to 8 digits; optionally dot and 1..6 ASCII fractional digits. Reject whitespace, plus, leading zeros, exponent and fraction longer than scale even when trailing zeros.
Use scaled integers or exact Decimal; no binary float. Output {"total":sum formatted with exactly scale fractional digits, or no dot when scale=0,"count":number_of_amounts}. Normalize negative zero.
This does not convert currencies, apply exchange rates, taxes or any financial policy.
The complete encoded input is limited to 20000 bytes; one additional byte can be read to detect overflow.
This is a bounded local helper. Passing the recorded cases does not verify every input, every Python version or an external integration.
Code license: MIT. Keep the license notice with your copy.
Example version 1.0.0 · Documentation reviewed 2026-11-17.
22 recorded CLI cases passed in rootless Docker on the pinned Python 3.12 runtime, with 512 MiB RAM, 1 CPU, no network, and read-only source. Other runtimes and all possible inputs are not verified.