Skip to content

Crypto payment amount precision: decimals and safe rounding

Prevent crypto payment amount errors with exact base units, explicit rounding rules and tests that keep prices, token amounts and display values separate.

Exact amountsSafe rounding

Handle crypto payment amounts as exact values with an explicit asset and unit. Never decide whether an order is paid by comparing rounded display text. A price, a quoted token amount and a blockchain transfer can describe the same purchase without being interchangeable numbers.

This guide is for developers building payment requests, order records and reconciliation. It explains how to prevent numeric errors before money moves. If funds already arrived with a shortfall or excess, use the underpayment and overpayment playbook instead. The controls below are merchant-side recommendations, not a claim that Yolfi exposes configurable precision or rounding settings.

Separate the three amounts in a purchase

Keep the commercial price, payment instruction and observed receipt distinct:

Amount Meaning What to preserve
Order price What the customer agreed to buy Exact price, pricing currency and order version
Invoice amount What the current payment attempt asks the buyer to send Quoted asset, network, exact amount and applicable expiry
Received amount What a verified matching transfer delivered Asset identity, exact units and transfer reference

A USD price is not automatically the same numeric amount of USDC, USDT or another asset. Use the provider's current invoice rather than recreating its quote from a displayed price. A formatted total such as two decimal places is a presentation choice, not proof of the underlying payment precision.

In Yolfi API Checkout, the merchant server supplies the final amount and currency for a Checkout Session. The buyer chooses an available network and token, and Yolfi creates the Invoice. Preserve that distinction in your records; do not replace the Invoice's crypto instructions with your own conversion. The server-controlled pricing guide covers order authority and session creation.

Understand token decimals before converting

For an asset with precision d, one whole token corresponds to 10^d base units. Convert by moving the decimal point exactly, not by estimating a floating-point product.

ERC-20 defines an optional decimals method. Do not assume every contract provides it or that every token uses the same value. Identify the asset by the relevant network and contract or native-asset identifier, then use verified metadata for that exact asset. A ticker alone is not enough.

These are illustrative arithmetic examples, not Yolfi-supported payment routes:

Verified precision Token amount Integer base units
6 decimals 19.99 19990000
6 decimals 0.000001 1
18 decimals 0.01 10000000000000000

OpenZeppelin explains the distinction between integer balances and display decimals. Asset precision also does not establish a payment service's allowed input precision, minimum payment or withdrawal limit. Check each interface separately.

Keep exact values through storage and transport

Choose an exact representation in your application: integer base units for token quantities, or a decimal type with an explicit scale. Size database columns for the supported range. Store the asset and scale alongside the amount, and compare quantities only after confirming their units match.

JavaScript needs particular care. MDN documents the safe integer limit for Number: 9007199254740991. The 18-decimal example above exceeds it even though the human-readable amount is only 0.01 tokens. Binary floating-point also cannot represent many decimal fractions exactly.

Use an appropriate exact-integer or decimal library for calculation. If you use JavaScript BigInt, define serialization explicitly: ordinary JSON serialization does not support BigInt values by default. Encode internal exact integers as decimal strings where your own interface specifies that contract.

This is not permission to change a provider's payload types. Follow each endpoint's documented field types and limits. Converting a value to a string after it has already lost precision cannot recover the missing digits. Check every boundary: browser input, server parsing, database writes, queues, exports and import jobs.

Decide rounding at the boundary, not during reconciliation

Write down which operation may round, to what scale and in which direction. There is no universally correct rule for every price, quote, fee and refund.

For a six-decimal asset, 1.0000004 cannot be represented exactly in base units. Reject excess precision or apply a deliberate, disclosed rule before creating the payment instruction. Do not silently truncate customer input and later describe the difference as a blockchain error.

For your own decimal parser:

  1. Validate a documented input grammar and supported range.
  2. Separate the whole and fractional digits without converting through floating point.
  3. Reject excess fractional digits unless that boundary has an explicit rounding rule.
  4. Pad the accepted fraction to the asset scale and form the exact integer.
  5. Format the result back and test that it represents the intended quantity.

Localize presentation separately. A customer may read a comma as the decimal separator; an API may require a period. Parse the supported local format deliberately, then emit the machine format the interface requires. Do not remove punctuation indiscriminately: 1,234 is ambiguous without a locale and input contract.

Never use display rounding as a payment tolerance

Suppose an invoice requires 1.000001 tokens and a matching receipt contains 1.000000. With six decimals, the difference is one base unit. Displaying both as 1.00 does not make them equal.

Compare exact values under the applicable payment-recognition rules. Keep any authorized merchant concession separate from provider payment status. Do not invent an epsilon that makes every small discrepancy count as paid, or assume a provider aggregates partial transfers.

Apply the same discipline to payment reconciliation: preserve the original receipt amount, record allocations separately and keep fees or adjustments identifiable. Formatting an export for a spreadsheet must not overwrite the exact ledger value.

Test precision failures before launch

Use controlled application fixtures, not deliberate wrong payments with real funds. Add these cases to the integration testing checklist:

Case Expected application result
Smallest supported base unit Exact conversion and round trip
One fractional digit beyond the allowed scale Explicit rejection or the documented rounding outcome
Value above the language's safe integer limit Exact storage and transport without Number coercion
Same displayed value, different exact amounts Difference remains visible to reconciliation logic
Local decimal separator and ambiguous grouping Accepted only under the documented input format
Same ticker with different asset metadata No reuse of an unrelated asset's precision
Export, then import of a large integer Original digits and asset identity survive unchanged

Also test configured minimums, maximums and zero or negative inputs. Record actual results for the implementation you ship; this checklist is not a claim that a particular provider accepts those values.

FAQ

Should every crypto amount have two decimal places?

No. A commercial price's display format is different from a token's base-unit precision and an API's allowed input scale. Preserve exact payment instructions even when a summary uses fewer digits.

Is using a decimal string enough?

Only if the value was exact before serialization and every consumer respects the format. A string containing an already-rounded number is still wrong. Validate scale, asset identity and range as well.

Can rounding fix an underpaid invoice?

Not by changing history. Preserve the exact shortfall and follow the documented exception process. An authorized concession is a separate business decision, not a change to the received transfer.

Conclusion: preserve exactness before asking for payment

Choose Yolfi payment links for suitable fixed-price offers or the documented API Checkout flow for dynamic orders. Keep the agreed price, current Invoice instructions and verified receipt distinct. Make units explicit, round only at defined boundaries and test the full storage-and-transport path before fulfillment depends on it.

Start accepting crypto payments today

Connect a wallet, create your first link and get paid in minutes.

Business owner working at a laptop
Payment received+120.00 USDT