Unit Price Calculation Precision
Unit Price value is stored and displayed using special "ACurrency" type, which stores 4 digits after decimal separator. However, if last 2 digits are equal to zero, they will not be shown. So, for example, if unit price equals to $ 20,0000, only $ 20,00 will be shown in CP. However, if unit price is $ 20,1234, then $ 20,1234 will be shown for this field in CP. A most typical use case for 4-digit after decimal separator format is calculation of low unit prices, for example for traffic, when unit price may be lower than 0,01.
Example of Unit Price calculation
Preconditions
Customer buys a service plan with:
- "Fixed number of years" billing period type set to 1 year
- Recurring price for this period of $ 20,00
- "Non-refundable amount" is 0
- "Full refund period" is 0 days
- "after full refund period" the refund is calculated as "prorated recurring fee".
Information about customer's purchase is stored in Billing, including the following:
- Price for the whole period = $ 20,00
- Price per month = $1,67 [rounded ($ 20)/(12 months)]
When 4 days passed after purchase, customer attempts to switch plan or cancel subscription.
Calculation
The order is generated with the refund line looking as follows:
ID | SKU | Description | Detail Type | Quantity | Duration | Unit Price | Discount | Extended Price |
---|---|---|---|---|---|---|---|---|
1 |
|
Refund for Plan 'dummy plan 3' for 0.99 year(s). |
Plan Recurring Refund |
-1.00 item |
0.99 year(s) |
$ 20.0371 |
$ 0.00 |
$ 19.82 |
How this refund line is calculated:
- We calculate the refund period we should pay for (in months):
- 361/365 days of year we multiply by 12 months
- Then we calculate Extended Price:
- (Price per month) x (refund period)
- Price per month of $1.67 that we store in Billing (see Preconditions above) is multiplied by the refund period of 11.869999999999999 calculated before and rounded to 2 digits after comma:
- Now, we calculate Unit Price:
- (Extended Price) / ( Duration x Quantity )
- Extended Price has been calculated above.
- Quantity for this case is 1, since this is a service plan, but and not a resource.
-
Duration is calculated as refund period / 12 (because we need duration in years here): 11.869999999999999 / 12 = 0.98916666666666664.
The refund period will be approximate 11.869999999999999 months.
Note: In this example we use approximate values for refund period calculation, because this is enough for our reasoning. In reality the refund period is calculated within the accuracy of seconds, and then it is converted into months. Thus, if we try to calculate the exact value for refund period, the result will slightly differ from the value above.
$1,67 x 11.869999999999999 = 19,82289999833 ~ 19.82 $.
This is the Extended Price, which is shown in CP.
So, Unit Price will be:
19.82 / (0.98916666666666664 x 1) ~ 20.0371, which is shown in CP.
Note: If the resulting Unit Price would be 20.0300, then we would only show two digits in decimal part ($ 20.03) in CP.