Code that calculates a unit price rounds it to the currency's Unit-Amount Rounding Precision
Business Central rounds a unit price only where its own code computes one, above all in the standard price calculation. Price Calculation Buffer Mgt.
- microsoftlayer
- data-modelingdomain
- 2examples
- 1pull request
Rule on GitHub · microsoft layer · data-modeling · BC all · tier official · derived
Contributions welcome — open a PR to refine or extend this article.
Description
Business Central rounds a unit price only where its own code computes one, above all in the standard price calculation. Price Calculation Buffer Mgt. converts a price list amount by tax, unit of measure, and currency, then calls RoundPrice, which rounds to the currency’s Unit-Amount Rounding Precision, or to the one in General Ledger Setup when the currency code is blank. Assigning or validating the field does not round it. The OnValidate of Sales Line.“Unit Price” and Purchase Line.“Direct Unit Cost” only re-validates Line Discount %. DecimalPlaces and AutoFormatType format the value for display but do not round a value that code assigns or validates. A computed price is therefore stored with every decimal it has.
The line amount is rounded: UpdateAmounts computes Round(Quantity * "Unit Price", Currency."Amount Rounding Precision"). Say a stored price of 49.7536 is displayed as 49.75. Four units then give a line amount of 199.01, while the printed document implies 4 × 49.75 = 199.00. Business Central reports no error. The difference shows up at the receiver, in an e-invoice validator, a customs declaration, or an EDI partner that recomputes quantity × price.
The widest gap is a subscriber that takes over the price lookup: Sales Line’s OnUpdateUnitPriceOnBeforeFindPrice or Purchase Line’s OnUpdateDirectUnitCostOnBeforeFindPrice with IsHandled := true. This skips the price calculation and its rounding together. The subscriber takes over the guarantees of the calculation it replaces (compare events/do-not-bypass-critical-operations-with-ishandled, which looks at the same pattern from the publisher’s side). Microsoft Learn says a currency’s unit-amount precision rounds “all unit amounts in the currency”. That describes the standard calculation and does not cover a price that code sets.
Not affected:
- A value copied unchanged from a field that is already rounded, for example another document line. BaseApp copies a blanket order line’s price this way.
- A price taken unchanged from an authoritative external document, such as an EDI order or a vendor invoice with four decimals. Rounding it would change the agreed price, so keep it and make the document layout show the full precision. An exact change of representation, such as minor currency units divided by 100, stays in this category.
- BaseApp’s
CalcUnitPriceUsingUOMCoef. For a credit memo copied from a posted invoice, it rescales the invoiced price to another unit of measure without rounding, to reproduce the posted price. - A price returned by the standard price calculation.
Best Practice
Round once, at the end, after every factor: markup, customer factor, unit of measure, and currency conversion. Use the precision of the document’s currency. Currency.Initialize(SalesHeader."Currency Code") loads the currency, and for a blank code it takes the precisions from General Ledger Setup. Then assign Round(Price, Currency."Unit-Amount Rounding Precision"). Rounding intermediate results adds rounding error. BaseApp follows the same rule when it computes a price itself: the Prices Including VAT conversion on Sales Header and the Unit Cost conversion on Sales Line both round to Unit-Amount Rounding Precision.
Prefer adjusting the standard result to replacing it. Let the price calculation run, change its price in OnUpdateUnitPriceByFieldOnAfterFindPrice, and round the adjusted value. BaseApp validates Unit Price after that event. A subscriber that must set IsHandled := true rounds the price before it assigns it. It also skips the standard conversion to the header’s VAT basis, so it either limits itself to documents without Prices Including VAT and leaves those to the standard calculation, as the sample does, or converts the price to the header’s basis before the single final Round (see data-modeling/document-line-prices-follow-prices-including-vat).
See sample: round-calculated-unit-prices-to-unit-amount-precision.good.al.
Anti Pattern
These shapes are the anti-pattern:
- Assigning or validating
Unit PriceorDirect Unit Costwith a value computed by*,/,ExchangeAmtLCYToFCY, orExchangeAmtFCYToLCY, without aRoundto the unit-amount precision of the line’s currency. - An
OnUpdateUnitPriceOnBeforeFindPriceorOnUpdateDirectUnitCostOnBeforeFindPricesubscriber that setsIsHandled := trueand assigns a price that is not rounded. This includes a price copied from a custom price table whose stored values are not rounded.
A hardcoded precision such as Round(Price, 0.01) is wrong for a currency whose unit-amount precision differs.
Do not report the cases listed under Not affected. Do not report code that already rounds the final price to Unit-Amount Rounding Precision.
See sample: round-calculated-unit-prices-to-unit-amount-precision.bad.al.
References
- BCApps:
Price Calculation Buffer Mgt.CalcUnitAmountRoundingPrecision,RoundPrice,ConvertAmount. - BCApps:
Sales Linefield 22Unit PriceOnValidate,UpdateUnitPriceByField,UpdateAmountsline amount,Unit Costrounding,CalcUnitPriceUsingUOMCoef. - BCApps:
Purchase Linefield 22Direct Unit CostOnValidate,UpdateDirectUnitCostByField. - BCApps:
Sales HeaderPrices Including VATconversion rounds the price. - BCApps:
Currency.InitializeandInitRoundingPrecision. - Set up currencies: unit-amount rounding: “The unit-amount rounding feature is used automatically every time you enter an item or resource number on a sales line.”
- DecimalPlaces property: “This setting is evaluated on text boxes and fields during validation.”
Examples
- Good: round-calculated-unit-prices-to-unit-amount-precision.good.al
- Bad: round-calculated-unit-prices-to-unit-amount-precision.bad.al
Names
- codeunit 7008 “Price Calculation Buffer Mgt.” (possibly related: a code span names it)
Changed by
From microsoft/BCQuality at a4593a1, MIT License, Copyright (c) 2026 Microsoft Corporation (license).