<p>Have you tried <code>subhead.hLevel.value</code>?</p>

CC BY-SA 4.0
2019-02-01T18:26:08.730Z
Doh! I am an idiot. That works. Thanks Olivier :)
Jonathan Schofield
CC BY-SA 4.0
2019-02-01T18:30:48.223Z
@JonathanSchofield Happened to me, that's the only reason I picked up on it ;) Glad I could help
Oli
CC BY-SA 4.0
2019-02-01T18:21:40.410Z
2019-02-01T18:21:40.410Z
Oli
<p>I had a <strong>dropdown</strong> field with a handle of <code>hLevel</code>, with only numeric values in it, anticipating that I could do math with the values in my template. But it seems I can't.</p>

<p><code>{% set dif = subhead.hLevel - subhead.next.hLevel %}</code></p>

<p>…returns an error message ending in:</p>

<blockquote>
  <p>“could not be converted to int”</p>
</blockquote>

<p>I found a thread convinced that <code>number_format</code> “<a href="https://stackoverflow.com/questions/24488946/intval-in-twig-template#comment81690544_24489675">converts a string to a number</a>”, so I tried that:</p>

<p><code>{% set dif = subhead.hLevel|number_format - subhead.next.hLevel|number_format %}</code></p>

<p>…but that doesn't work either. The error message changes to:</p>

<blockquote>
  <p>“could not be converted to double”</p>
</blockquote>

<p>Which doesn't surprise me as I did think that <code>number_format</code> is for float to formatted string conversion, not the other way round as well.  ¯\_(ツ)_/¯</p>

<p>When I change the field format to <strong>number</strong> everything works fine, but…</p>

<p>It would be good if there is an option to have <strong>dropdown</strong> field values be <strong>parsed as numbers</strong>, because from a UX perspective I would prefer that the field is a dropdown.</p>

<p>Or is there some workaround I've missed?</p>

<drop-down><number><math>
Math with numeric values derived from a dropdown field
# Math with numeric values derived from a dropdown field

(Asked by: username_0 on 2019-02-01)

I had a **dropdown** field with a handle of `hLevel`, with only numeric values in it, anticipating that I could do math with the values in my template. But it seems I can't.

`{% set dif = subhead.hLevel - subhead.next.hLevel %}`

…returns an error message ending in:

> “could not be converted to int”

I found a thread convinced that `number_format` “[converts a string to a number](https://stackoverflow.com/questions/24488946/intval-in-twig-template#comment81690544_24489675)”, so I tried that:

`{% set dif = subhead.hLevel|number_format - subhead.next.hLevel|number_format %}`

…but that doesn't work either. The error message changes to:

> “could not be converted to double”

Which doesn't surprise me as I did think that `number_format` is for float to formatted string conversion, not the other way round as well. ¯\\_(ツ)\_/¯

When I change the field format to **number** everything works fine, but…

It would be good if there is an option to have **dropdown** field values be **parsed as numbers**, because from a UX perspective I would prefer that the field is a dropdown.

Or is there some workaround I've missed?

**Tags:** drop-down, number, math


---

## 1 answers

### Answer by username_1 on 2019-02-01. Score: 5

Have you tried `subhead.hLevel.value`?

> **Comments:**
> **username_0**: Doh! I am an idiot. That works. Thanks username_1vier :)  
> **username_1**: @JonathanSchofield Happened to me, that's the only reason I picked up on it ;) Glad I could help  

---

