Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-11T06:33:45.403Z
Answers.Text: hmmm... I'm getting error: Twig Runtime Error – Twig\Error\RuntimeError
  Variable "fields" does not exist.
Answers.UserDisplayName: Lukas Snarskis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-11T08:34:42.677Z
Answers.Text: @LukasSnarskis try to copy/paste it from above again, there was just a couple of comments not closed properly.
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-11T23:36:07.667Z
Answers.Text: Oops... thanks @Oli
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-10T23:15:30.957Z
Answers.LastActivityDate: 2021-03-11T08:33:32.270Z
Answers.LastEditDate: 2021-03-11T08:33:32.270Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <entry><field>
Title: Getting average of number field values
ThreadText: # 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:
  
  ![Text](https://i.sstatic.net/ewHRT.png)
  
  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 %}
  
  <strong>{{ratingsSum|number(decimals=1)}}</strong>
  
  ```
  
  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**: @username_0 try to copy/paste it from above again, there was just a couple of comments not closed properly.  
  > **username_1**: Oops... thanks @username_2  
  
  ---
  
  