<p>When working with a dropdown in Twig, it's important to remember that you're dealing with an <strong>object</strong> (not a simple string).</p>

<pre><code>This is an object:
{{ entry.myDropdown }}
</code></pre>

<p>It's easy to forget, however, because Craft works a little magic behind the scenes. There are many objects (including this one) which have an internal <code>__toString</code> behavior. This is called a "magic method", because it will automatically convert the value to a string <em>when you use it as a string</em>.</p>

<p>But in your case, you're not directly outputting it as a string. You're trying to do math with the object, and the object isn't really sure how to respond to that. So nothing gets converted, and your object remains mathematically useless.</p>

<p>Instead, you'll need to append <code>.value</code> to your object. This will give you the <strong>selected value</strong> of your dropdown, in a format that you can use.</p>

<pre><code>This is the dropdown's selected value:
{{ entry.myDropdown.value }}
</code></pre>

CC BY-SA 4.0
2018-06-15T02:23:04.007Z
SO HELPFUL! I've been so confused by this until this answer.
Johann Dyck
CC BY-SA 3.0
2016-08-25T22:39:09.437Z
2016-08-25T22:39:09.437Z
Lindsey D
<p>I am storing a value (number 1-5) in a drop down field and would like to do a calculation based on the numbers filled in.  The only way I could get my calculation to work was to store the number (1-5) in a number field (not a drop down field) so I'm wondering if it's because the drop down content is seen as a string and not an integer. If so, perhaps I need to convert the string to an integer.  Although I have it working with the value (1-5) stored in a number field, I'd like to understand how I could make it work with a drop down.  I saw another post that sounded similar: <a href="https://craftcms.stackexchange.com/questions/141/how-can-i-convert-a-dropdown-field-value-to-an-integer-within-a-hash">How can I convert a dropdown field value to an integer within a hash?</a></p>

<p>Here is my template code that works with my number field:</p>

<pre><code>&lt;!-- AORTA --&gt;
    &lt;!-- Get entries for study type Aorta for logged in sonographer. Filter out any scores that are default zero. --&gt;
        {% set imageQualityScoreAorta = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logGeneral_studyType:aorta -logReviewer_imageQualityNumberTest:0').relatedTo(
            { targetElement: currentUser, field: 'logGeneral_sonographerName' }
        ) %}

    &lt;!-- Get average image quality score IF there are any --&gt;
        {% if imageQualityScoreAorta|length %}
            &lt;!-- Set default numbers --&gt;
            {% set additionAorta = 0 %}
            {% set countAorta = 0 %}

            &lt;!-- Loop --&gt;
            {% for entry in imageQualityScoreAorta %}
                {% set additionAorta = additionAorta + entry.logReviewer_imageQualityNumberTest %}&lt;/td&gt;
                {% set countAorta = loop.index %}
            {% endfor %}

            &lt;!-- Get average image quality score --&gt;
            {% set averageValueAorta = (additionAorta / countAorta)|round(1, 'floor') %}
        {% endif %}
</code></pre>

<p>This is the output:</p>

<pre><code>&lt;!-- Current User Total Studies --&gt;
&lt;table class="table"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td class="c-font-uppercase c-font-bold" scope="row"&gt;Aorta&lt;/th&gt;
            {% if imageQualityScoreAorta|length %}
                &lt;td class="c-center c-font-bold"&gt;{{ averageValueAorta }}&lt;/td&gt;
            {% else %}
                &lt;td class="c-center c-font-bold"&gt;N/A&lt;/td&gt;
            {% endif %}
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
</code></pre>

<drop-down><number><integer>
Calculating a score based on a drop down field value
# Calculating a score based on a drop down field value

(Asked by: username_0 on 2016-08-25)

I am storing a value (number 1-5) in a drop down field and would like to do a calculation based on the numbers filled in.  The only way I could get my calculation to work was to store the number (1-5) in a number field (not a drop down field) so I'm wondering if it's because the drop down content is seen as a string and not an integer. If so, perhaps I need to convert the string to an integer.  Although I have it working with the value (1-5) stored in a number field, I'd like to understand how I could make it work with a drop down.  I saw another post that sounded similar: How can I convert a dropdown field value to an integer within a hash?
Here is my template code that works with my number field:

    
        {% set imageQualityScoreAorta = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logGeneral_studyType:aorta -logReviewer_imageQualityNumberTest:0').relatedTo(
            { targetElement: currentUser, field: 'logGeneral_sonographerName' }
        ) %}

    
        {% if imageQualityScoreAorta|length %}
            
            {% set additionAorta = 0 %}
            {% set countAorta = 0 %}

            
            {% for entry in imageQualityScoreAorta %}
                {% set additionAorta = additionAorta + entry.logReviewer_imageQualityNumberTest %}
                {% set countAorta = loop.index %}
            {% endfor %}

            
            {% set averageValueAorta = (additionAorta / countAorta)|round(1, 'floor') %}
        {% endif %}

This is the output:


    
        
            Aorta
            {% if imageQualityScoreAorta|length %}
                {{ averageValueAorta }}
            {% else %}
                N/A
            {% endif %}

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

---

## 1 answers

### Answer by username_1 on 2016-08-25. Score: 3 | Accepted answer

When working with a dropdown in Twig, it's important to remember that you're dealing with an object (not a simple string).
This is an object:
{{ entry.myDropdown }}

It's easy to forget, however, because Craft works a little magic behind the scenes. There are many objects (including this one) which have an internal __toString behavior. This is called a "magic method", because it will automatically convert the value to a string when you use it as a string.
But in your case, you're not directly outputting it as a string. You're trying to do math with the object, and the object isn't really sure how to respond to that. So nothing gets converted, and your object remains mathematically useless.
Instead, you'll need to append .value to your object. This will give you the selected value of your dropdown, in a format that you can use.
This is the dropdown's selected value:
{{ entry.myDropdown.value }}

> **Comments:**
> **username_2**: SO HELPFUL! I've been so confused by this until this answer.  

---

