> For the complete documentation index, see [llms.txt](https://docs.datajet-app.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.datajet-app.com/liquid/filters/integer.md).

# integer

Casts a value to a whole number. Use it when a value arrives as a string — from a CSV column, a metafield, a webhook payload, a URL parameter — but has to be a real number downstream.

```liquid
{% assign quantity = row.qty | integer: 0 %}
```

## Syntax

```liquid
{{ value | integer }}
{{ value | integer: fallback }}
```

| Parameter  | Description                                                          |
| ---------- | -------------------------------------------------------------------- |
| `value`    | The value to cast. Numbers and decimal strings are accepted          |
| `fallback` | Optional. Returned when the value is not a number. Defaults to `nil` |

## Return Value

Returns a number truncated toward zero, or the fallback (`nil` by default) when the value cannot be cast.

## Examples

**Cast a CSV column before comparing or summing:**

```liquid
{% assign rows = file.content | parse_csv %}
{% for row in rows %}
  {% assign qty = row.quantity | integer: 0 %}
  {% if qty > 0 %}
    {% assign total = total | plus: qty %}
  {% endif %}
{% endfor %}
```

**Build a JSON payload that needs a real number:**

```liquid
{% assign id = params.variant_id | integer %}
{% json %}
{ "variantId": {{ id | json }}, "quantity": {{ "3" | integer | json }} }
{% endjson %}
```

Output:

```json
{ "variantId": 45123456789, "quantity": 3 }
```

**Guard against bad input:**

```liquid
{% if row.qty | is_number %}
  {% assign qty = row.qty | integer %}
{% else %}
  {% log "Skipping row, quantity is not a number: " | append: row.qty %}
  {% continue %}
{% endif %}
```

## Notes

* Truncates toward zero: `"12.9" | integer` is `12`, `"-12.9" | integer` is `-12`. Use `round` first if you want rounding
* Stricter than `| plus: 0` on purpose. These all return the fallback instead of a wrong number:

  | Input            | `\| plus: 0` | `\| integer` |
  | ---------------- | ------------ | ------------ |
  | `"0x1F"`         | `31`         | fallback     |
  | `"1e3"`          | `1000`       | fallback     |
  | `"1,234"`        | `NaN`        | fallback     |
  | `"abc"`          | `NaN`        | fallback     |
  | `""`             | `0`          | fallback     |
  | missing variable | `NaN`        | fallback     |
  | `true`           | `1`          | fallback     |
* With no fallback the result is `nil`, which renders as an empty string and works with `default` — unlike `NaN`, which renders literally and corrupts a JSON payload
* Leading zeros and surrounding whitespace are fine: `" 007 " | integer` is `7`
* Values beyond JavaScript's safe integer range (16+ digits) lose precision. Keep those as strings with the `string` filter
* See also: `decimal`, `is_number`, `string`, `type`
