OOmnistu

Blog / Web & Ecommerce

Web & Ecommerce

Money input in an app: test the decimal boundary before multiplying by 100

Test canonical money text before minor-unit conversion. Executed Python fixtures cover USD/JPY scale, refused input and explicit rounding boundaries.

OOmnistu
Reviewed 2026-11-18 · 5 min read
Six accepted text-money fixtures, including zero, map to integer minor units; twenty-one inputs are refused under a limited USD and JPY sample policy.
Original Omnistu diagram of the article fixture. Scroll horizontally on a small screen. These synthetic test results do not certify a live system.

A payment amount can look right on screen and still be wrong at the API boundary. The application might accept more decimal places than intended, convert through a binary float, or assume every currency uses the same scale. These defects belong in small deterministic tests before any payment request is made.

This article implements a deliberately limited text-to-integer conversion for two sample currencies. It does not calculate taxes, authorize charges or establish a complete payment-provider contract. The fixtures are synthetic and make no network requests.

Define the input contract first

For the example, the product accepts nonnegative ASCII decimal text with at most six digits before the decimal point. USD allows one or two fractional digits; JPY allows none. No sign, grouping separator, scientific notation, surrounding whitespace or leading zeroes except the single zero are accepted.

That strict format is an example policy, not a universal recommendation for customer-facing forms. A localized interface might accept other separators or digit shapes. If so, normalize them under a separately specified rule before entering the canonical amount boundary. Never let the server guess whether 1,000 means one thousand or one with three decimal places.

The sample recognizes only USD and JPY. It rejects every other currency rather than silently treating an unknown code as two-decimal.

Preserve decimal text until parsing

Python's decimal documentation explains that converting a float to Decimal exactly preserves the float's existing binary value. It does not recover the original decimal text that the customer typed. In the executed fixture, Decimal(0.1) differs from Decimal('0.1').

The boundary therefore requires text and constructs Decimal directly from it. A later str() call on an already rounded or altered float is not a reliable recovery strategy for a lost original value.

import re
from decimal import Decimal, localcontext

PLACES = {"USD": 2, "JPY": 0}


def to_minor(text, currency):
    if not isinstance(currency, str) or currency not in PLACES:
        raise ValueError("unsupported currency")
    if not isinstance(text, str):
        raise ValueError("amount must be text")
    places = PLACES[currency]
    suffix = rf"(?:\.[0-9]{{1,{places}}})?" if places else ""
    if not re.fullmatch(r"(?:0|[1-9][0-9]{0,5})" + suffix, text):
        raise ValueError("amount format outside sample policy")
    with localcontext() as ctx:
        ctx.prec = 28
        return int(Decimal(text) * (10 ** places))

The format bounds keep this example's multiplication within the selected precision. int() is not being used as an accidental rounding rule: accepted input already has no fraction of a minor unit. Inputs such as 1.001 USD are refused before conversion.

The boundary also rejects non-text currency fields and unrecognized letter case. A real request schema should state its own canonical currency-code convention and validate the supported operation.

Test accepted values and refusals

The script ran on Python 3.10.12. Six accepted fixtures included 10 USD to 1000, 10.10 USD to 1010, 0.29 USD to 29, and 10 JPY to 10. The upper sample boundary, 999999.99 USD, became 99999999.

Twenty-one refusal fixtures covered extra precision, negative or signed amounts, leading zeroes, whitespace, NaN, Infinity, exponent notation, comma notation, an oversized value, unsupported or malformed currency codes, non-text amounts and non-ASCII digits. The tests asserted the expected integer or required the conversion to raise ValueError.

Accepting 0 here only proves that the parser can represent zero. It does not mean a provider accepts a zero charge. Provider minimums, maximums, currency availability, operation type and business permissions are separate checks after parsing.

The provider's scale is part of the contract

Stripe's currency documentation gives 1000 for a 10 USD amount and 10 for 10 JPY. It also documents special cases for particular currencies and differences between charges and payouts. This is why the example does not attempt to construct a universal currency table from a two-decimal default.

Read the exact endpoint's amount definition and the current currency rules. Keep the conversion policy versioned and test the currencies and operations the product actually supports. Do not infer supported currencies or commercial limits from this sample dictionary.

Always carry the currency with the integer. An unlabelled integer such as 1000 does not explain the economic amount on its own.

Rounding is a separate business decision

Rejecting excessive input precision is different from rounding calculated results. Discounts, allocations and percentage calculations may produce additional decimal places even when the entered price was valid.

The fixture illustrates this distinction with three synthetic values of 0.005 and an explicit ROUND_HALF_EVEN rule. Rounding each line to two places and then adding gives 0.00. Adding first and rounding once gives 0.02. Both results follow their stated arithmetic sequence; neither sequence is automatically the correct invoice policy.

Agree where rounding happens, which mode applies and how any allocation remainder is handled. Legal, tax and accounting requirements may determine those choices. This guide supplies no jurisdiction-specific rounding advice.

Acceptance evidence for a payment feature

Trace the same amount and currency through entry, canonical parsing, calculation, stored order, provider request, provider result and customer receipt in supported test mode. Include a request that bypasses browser validation. A formatted display should never be the sole source used to build a charge.

The parser test is complete when it demonstrates its narrow contract and rejects what lies outside it. The payment feature is not complete until its provider-specific limits, reconciliation and authorization rules are tested separately.

Sources and method

Written with AI assistance. The accompanying synthetic parser and rounding fixtures were executed locally. No funds, real payment methods or customer orders were involved.