When submitting a request in the Unleashed API for a Sales ir Purchase Order, ensuring accurate monetary field calculations is essential to avoid validation errors and floating-point precision issues.
Common Issues with Monetary Calculations in API Requests
Validation Errors for Totals: These occur when Unleashed's expected calculations for totals and tax amounts do not match the values submitted in the payload. Such errors typically result from incorrect rounding or incomplete calculations.
Floating-Point Precision Errors: Monetary fields submitted with inconsistently rounded values or excessive decimal precision trigger calculation misalignments, leading to errors.
Required Calculation Steps to Avoid Validation Errors
To ensure your sales order passes Unleashed's validations, include the following correctly calculated and rounded fields:
Line-Level Calculations
For each order line:
LineTotal: Calculate as OrderQuantity Γ UnitPrice, rounded to 2 decimal places.
LineTax: Calculate as LineTotal Γ TaxRate, rounded to 2 decimal places.
Order-Level Calculations
For the entire sales order:
SubTotal: Sum of all LineTotal values.
TaxTotal: Sum of all LineTax values.
Total: Sum of SubTotal and TaxTotal.
Use these formulas and rounding rules consistently to ensure Unleashed's calculations align with the submitted payload.
Best Practices for Avoiding Floating-Point Precision Errors
Exact Decimal Arithmetic: Perform all calculations using exact decimal arithmetic instead of floating-point numbers to eliminate rounding discrepancies.
Consistent Rounding: Apply rounding rules (to two decimal places) at every calculation step to ensure that component sums match their corresponding totals.
Avoid Inconsistent Values: Do not submit imprecise or partially calculated values. Ensure all derived fields, like SubTotal, TaxTotal, and Total, are computed directly based on line amounts.
By adhering to these steps and practices, you can reliably submit error-free sales orders via the Unleashed API.
