<p>Something like this should work:</p>
<pre><code>{# Setup an arry of the field handles you're interested in #}
{% set fields = ['num1', 'num2', 'num3'] %}

{# set some defaults #}
{% set totalAmount = 0 %}
{% set totalFields = 0 %}

{# loop through the field array #}
{% for field in fields %}

    {# grab the field's value from the entry #}
    {% set value = attribute(entry, field) %}

    {# skip over fields that were left blank, but count fields where 0 was entered #}
    {% if value is not empty and value &gt;= 0 %}

        {# add to the running total and running number of fields with values #}
        {% set totalAmount = totalAmount + value %}
        {% set totalFields = totalFields + 1 %}

    {% endif %}
{% endfor %}

{# do some math #}
{% if totalFields &gt; 0 %}
    {{ (totalAmount / totalFields)|number(decimals=1) }}
{% endif %}
</code></pre>

CC BY-SA 4.0
2021-03-11T06:33:45.403Z
hmmm... I'm getting error: Twig Runtime Error – Twig\Error\RuntimeError
Variable "fields" does not exist.
Lukas Snarskis
CC BY-SA 4.0
2021-03-11T08:34:42.677Z
@LukasSnarskis try to copy/paste it from above again, there was just a couple of comments not closed properly.
Oli
CC BY-SA 4.0
2021-03-11T23:36:07.667Z
Oops... thanks @Oli
Brad Bell
CC BY-SA 4.0
2021-03-10T23:15:30.957Z
2021-03-11T08:33:32.270Z
2021-03-11T08:33:32.270Z
Brad Bell
<p>I'm getting close to finishing my first project on Craft CMS but I have one problem.</p>
<p>I'm using number fields to rate items in my entries. It looks like this:</p>
<p><img src="https://i.sstatic.net/ewHRT.png" alt="Text" /></p>
<p>I will have around 6-7 fields and I need to get the average rating of them to display in the front end.</p>
<pre><code>{% set ratingAverage = entry.dizainas + entry.ekranas + entry.veikimas %}

{% set ratingsSum = ratingAverage / 3 %}

&lt;strong&gt;{{ratingsSum|number(decimals=1)}}&lt;/strong&gt;
</code></pre>
<p>In fact, some of those fields might be left empty as well so it's needed to skip them.</p>
<p>Can someone help me out with this?</p>

<entry><field>
Getting average of number field values
# Getting average of number field values

(Asked by: username_0 on 2021-03-10)

I'm getting close to finishing my first project on Craft CMS but I have one problem.
I'm using number fields to rate items in my entries. It looks like this:

I will have around 6-7 fields and I need to get the average rating of them to display in the front end.
{% set ratingAverage = entry.dizainas + entry.ekranas + entry.veikimas %}

{% set ratingsSum = ratingAverage / 3 %}

{{ratingsSum|number(decimals=1)}}

In fact, some of those fields might be left empty as well so it's needed to skip them.
Can someone help me out with this?

**Tags:** entry, field

---

## 1 answers

### Answer by username_1 on 2021-03-10. Score: 2 | Accepted answer

Something like this should work:
{# Setup an arry of the field handles you're interested in #}
{% set fields = ['num1', 'num2', 'num3'] %}

{# set some defaults #}
{% set totalAmount = 0 %}
{% set totalFields = 0 %}

{# loop through the field array #}
{% for field in fields %}

    {# grab the field's value from the entry #}
    {% set value = attribute(entry, field) %}

    {# skip over fields that were left blank, but count fields where 0 was entered #}
    {% if value is not empty and value >= 0 %}

        {# add to the running total and running number of fields with values #}
        {% set totalAmount = totalAmount + value %}
        {% set totalFields = totalFields + 1 %}

    {% endif %}
{% endfor %}

{# do some math #}
{% if totalFields > 0 %}
    {{ (totalAmount / totalFields)|number(decimals=1) }}
{% endif %}

> **Comments:**
> **username_0**: hmmm... I'm getting error: Twig Runtime Error – Twig\Error\RuntimeError Variable "fields" does not exist.  
> **username_2**: @LukasSnarskis try to copy/paste it from above again, there was just a couple of comments not closed properly.  
> **username_1**: Oops... thanks @Oli  

---

