LanguageValues and typesStandard typesMoney and banking
Discount
On this page
An amount off a price, or a percentage off it.
Use it for promotions and vouchers. money.apply_discount takes one off a Price.
Shape
One of these, each with its own fields:
| Variant | Field | Type | Required? |
|---|---|---|---|
Discount.amount_off |
amount |
Money |
Yes |
Discount.percentage_off |
percentage |
Percentage |
Yes |
Example
Discount.percentage_off { percentage: 20% }Good to know
- The other variant is
Discount.amount_off { amount: ... }. - An amount off can't be below zero, and a percentage off runs from 0% to 100%.
==tells you whether two are the same.<andlist.sort_bydon't take it.
In JSON
An object of its variant's name, as tag, and the variant's fields, in a request, a response or json.write:
{"tag":"amount_off","amount":{"amount":500,"currency":"GBP"}}{"tag":"percentage_off","percentage":"20%"}{"tag":"percentage_off","percentage":"12.5%"}
Details
Text form, storage, how it is shown, and operations
Discount is in the group Money and banking, and has been in Jadpo since 0.1.0.
Text form
It has no single text form, so text.from and text.parse don't take it.
How it is stored
It is stored in 4 columns, each named for the field, _ and its suffix. This is {"tag":"percentage_off","percentage":"20%"}:
| Column | Type | Holds |
|---|---|---|
<field>_kind |
text |
'percentage_off' |
<field>_amount |
bigint |
NULL::bigint |
<field>_currency |
char(3) |
NULL::text |
<field>_percentage |
numeric |
0.20 |
How it is shown to people
text.format(value, locale:) shows it to people in their locale.
With locale: Locale.en_gb:
| Value | Shown |
|---|---|
{"tag":"amount_off","amount":{"amount":500,"currency":"GBP"}} |
£5.00 |
{"tag":"percentage_off","percentage":"20%"} |
20% |
{"tag":"percentage_off","percentage":"12.5%"} |
12.5% |
Operations
money.apply_discount(price: Price, discount: Discount) -> Price:pricewithdiscounttaken off its amount, keeping whether it includes tax: an amount off in the price's currency, or that percentage of the amount, rounded half to even to a minor unit, so 15% off £10.05 is £1.51 off. More off than the price leaves nothing. It fails withcurrency_mismatchfor an amount off in another currency.
It also takes text.format, json.read, json.write and ==. How every standard type behaves says how they work.
Markdown for agents: /docs/types/type-discount.md.Built from Jadpo main at aee2e8f.