# `UnitQuantity`

How much of something: a count of items, or an amount by mass, volume, length or area.

Use it on an order line or a stock count that sells both "3 items" and "2.5 kg". If you sell only whole items, your own type on `Int` is simpler.

## Shape

One of these, each with its own fields:

| Variant | Field | Type | Required? |
|---|---|---|---|
| `UnitQuantity.each` | `count` | `Int` | Yes |
| `UnitQuantity.mass` | `amount` | [`Mass`](/docs/types/type-mass.md) | Yes |
| `UnitQuantity.volume` | `amount` | [`Volume`](/docs/types/type-volume.md) | Yes |
| `UnitQuantity.length` | `amount` | [`Length`](/docs/types/type-length.md) | Yes |
| `UnitQuantity.area` | `amount` | [`Area`](/docs/types/type-area.md) | Yes |

## Example

```jadpo
UnitQuantity.each { count: 3 }
```

## Good to know

- The others are `.mass`, `.volume`, `.length` and `.area`, each with an `amount`: `UnitQuantity.mass { amount: 2.5kg }`.
- It can't 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":"each","count":3}
{"tag":"each","count":1200}
{"tag":"mass","amount":{"value":"2.5","unit":"kg"}}
```

## Details

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

`UnitQuantity` is in the group Commerce and logistics, 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 3 columns, each named for the field, `_` and its suffix. This is `{"tag":"volume","amount":{"value":"0.0015","unit":"cubic_metre"}}`:

| Column | Type | Holds |
|---|---|---|
| `<field>_kind` | `text` | `'volume'` |
| `<field>_count` | `bigint` | `NULL::bigint` |
| `<field>_amount` | `numeric(38, 9)` | `0.0015` |

### 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":"each","count":3}` | `3` |
| `{"tag":"each","count":1200}` | `1,200` |
| `{"tag":"mass","amount":{"value":"2.5","unit":"kg"}}` | `2.5 kg` |
| `{"tag":"volume","amount":{"value":"0.0015","unit":"cubic_metre"}}` | `0.0015 m³` |
| `{"tag":"length","amount":{"value":"12","unit":"metre"}}` | `12 m` |
| `{"tag":"area","amount":{"value":"20","unit":"sqm"}}` | `20 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>
