A fabric shop records that it received 0.3 metres from a sample roll and reserved pieces of 0.1 and 0.2 metres. Its new Python import, written with help from an AI assistant, reports a tiny shortage instead of zero. The quantities on the screen look right. The surprise is in the code: it reads the values as ordinary floats and converts them to Decimal later. By then, it is preserving the values Python stored rather than the numbers the shop originally wrote.
For a stock rule that treats the recorded tenths as exact, the useful question is where the conversion happens. If you still have the source text, use it. If you only have a float, decide what that value means before trying to make it look tidy.
Decimal preserves what it receives
The Python decimal reference gives a telling example. The decimal made from the text '0.1' represents one tenth exactly. The decimal made from the float 0.1 represents the exact value of that already approximated binary number, which is slightly larger than one tenth. A display that prints just 0.1 does not remove the difference.
That behaviour is useful if you want to inspect the value a float actually holds. It is the wrong starting point if the agreed rule is to add recorded decimal tenths. In the example, adding exact decimal quantities gives a reservation of 0.3 metres and a remaining recorded balance of zero. The result says nothing about how accurately the shop measured the cloth. It says the calculation respected the numbers the shop chose to record.
This also explains why a cleanup like Decimal(str(value)) deserves a closer look. If a float came directly from a simple decimal, its string may look like the original. If you have added two floats first, the string can carry the result of that arithmetic instead. And once the original writing has been lost, the value alone cannot tell you whether the source said 0.1 or 0.10. If the written scale matters to the workflow, preserve it at the start.
Decide how numbers enter the script
Trace a single quantity from the incoming file to the first calculation. For a comma-separated export that gives the quantity as text, pass that text to Decimal after checking that it fits the file’s expected format. Decide how the shop handles a blank cell, a unit written alongside the number, or a decimal comma from a different export. Those are questions about the source format. They deserve a clear rule rather than a guess inside the arithmetic.
If a system sends JSON with numeric quantities, Python’s JSON decoder documentation describes the parse_float option. Passing Decimal there lets the decoder construct the value from the number’s text before it becomes a binary float. It applies to JSON numbers with a fractional part or exponent, rather than to numeric strings or ordinary integers. That means the import still needs a policy for quantities sent as text, and for any other parts of the program that expect floats.
If all you receive is an existing float, ask whether the system can expose the original input or its documented decimal scale. Sometimes a business deliberately rounds incoming measurements to a fixed unit before storing them. If that is the rule, document the rounding step and test it. Choosing a conversion merely because the output looks familiar makes it harder for the next person to know which data they are using.
Keep rounding separate from parsing. A report might display stock to one decimal place while the underlying records include hundredths. If a cut list uses those hundredths, rounding each record on arrival can change which orders appear to fit. Write down whether rounding is part of the stock rule or just a display choice. Python’s decimal arithmetic also has a precision context, so using Decimal is not a promise of unlimited precision. For this small example, the arithmetic fits comfortably; a more demanding calculation should test the precision it needs.
Verify the calculation and the handoff
Try a few small cases whose answers someone can check by hand. In particular, test the file ingestion path rather than constructing ideal decimal values directly in the calculation test. Otherwise the importer can introduce a float even though every example in the test suite passes.
- Use a balance that should reach zero. Read 0.3, 0.1 and 0.2 from a representative fixture and check the recorded balance after both reservations.
- Include one value with a trailing zero. If the written scale matters, verify that the import and output handle 0.10 as agreed.
- Try a blank and a malformed value. Check that each produces the intended result, such as a clear rejection rather than an invisible zero.
- Inspect the downstream handoff. Confirm that the spreadsheet or service receiving the result interprets it correctly. Agree whether the transfer uses decimal text, whole units on a fixed scale or another documented format.
Exact recorded quantities and approximate physical measurements call for different decisions. When the task is to decide whether a noisy measurement is close enough to zero, the related MaryChuks article on choosing a tolerance near zero explains a different approach. For recorded quantities that are meant to add exactly, start by keeping the original decimal input intact. In code review, ask one concrete question: at what point did the value first become a number, and what kind of number was it then?
Sources
- Python 3.14 standard library: decimal and conversion from floats.
- Python 3.14 standard library: JSON decoding and parse_float.
Discover more from Marychuks.com AI, Psychology, Business & CreativeVerse
Subscribe to get the latest posts sent to your email.