# `UnitPrice`

A price per amount of something, such as £2.50 per kilo.

Use it for goods sold by weight, volume, length or area, as UK shelf labels show them. For the price of a thing you count, use [`Price`](/docs/types/type-price.md).

## Shape

One of these, each with its own fields:

| Variant | Field | Type | Required? |
|---|---|---|---|
| `UnitPrice.per_mass` | `price` | [`Money`](/docs/types/type-money.md) | Yes |
|  | `per` | [`Mass`](/docs/types/type-mass.md) | Yes |
| `UnitPrice.per_volume` | `price` | [`Money`](/docs/types/type-money.md) | Yes |
|  | `per` | [`Volume`](/docs/types/type-volume.md) | Yes |
| `UnitPrice.per_length` | `price` | [`Money`](/docs/types/type-money.md) | Yes |
|  | `per` | [`Length`](/docs/types/type-length.md) | Yes |
| `UnitPrice.per_area` | `price` | [`Money`](/docs/types/type-money.md) | Yes |
|  | `per` | [`Area`](/docs/types/type-area.md) | Yes |

## Example

```jadpo
UnitPrice.per_mass { price: money.from_minor(250, Currency.gbp) per: 1kg }
```

## Good to know

- Pick the variant by what you measure: `per_mass`, `per_volume`, `per_length` or `per_area`.
- Neither the price nor the amount can be below zero.
- `==` tells you whether two are the same. `<` and `list.sort_by` don'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`:

```json
{"tag":"per_mass","price":{"amount":250,"currency":"GBP"},"per":{"value":"1","unit":"kg"}}
{"tag":"per_volume","price":{"amount":180,"currency":"GBP"},"per":{"value":"0.001","unit":"cubic_metre"}}
{"tag":"per_length","price":{"amount":399,"currency":"GBP"},"per":{"value":"1","unit":"metre"}}
```

## Details

<details>
<summary>Text form, storage, how it is shown, and operations</summary>

`UnitPrice` 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":"per_volume","price":{"amount":180,"currency":"GBP"},"per":{"value":"0.001","unit":"cubic_metre"}}`:

| Column | Type | Holds |
|---|---|---|
| `<field>_kind` | `text` | `'per_volume'` |
| `<field>_price` | `bigint` | `180` |
| `<field>_price_currency` | `char(3)` | `'GBP'` |
| `<field>_per` | `numeric(38, 9)` | `0.001` |

### 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":"per_mass","price":{"amount":250,"currency":"GBP"},"per":{"value":"1","unit":"kg"}}` | `£2.50/1 kg` |
| `{"tag":"per_volume","price":{"amount":180,"currency":"GBP"},"per":{"value":"0.001","unit":"cubic_metre"}}` | `£1.80/0.001 m³` |
| `{"tag":"per_length","price":{"amount":399,"currency":"GBP"},"per":{"value":"1","unit":"metre"}}` | `£3.99/1 m` |

### Operations

It also takes `text.format`, `json.read`, `json.write` and `==`. [How every standard type behaves](/docs/every-standard-type-meets-one-contract.md) says how they work.

</details>
