<p>You can try the following code</p>

<pre><code>{% set ratingsSum = 0 %}
{% for order in pastOrders %}
    {% set ratingsSum = ratingsSum + order.orderDeliveryTime.value %}
{% endfor %}

{% set ratingAverage = ratingsSum / pastOrders | length %}
AverageTime:{{ratingAverage}}
</code></pre>

<p>Difference between yours and my code is i have added <strong>.value</strong> with <strong>order.orderDeliveryTime</strong></p>

CC BY-SA 3.0
2017-03-24T08:00:42.913Z
2017-03-24T08:00:42.913Z
hashtagerrors
<pre><code>1.{% set totalDeliveryTime = 0 %}
2.{% set countDeliveries = 0 %}
3.{% set averageDeliveryTime = 0 %}    
4.{% for order in pastOrders %}
5. to--{{order.orderDeliveryTime}}--
6.{% set totalDeliveryTime = totalDeliveryTime + order.orderDeliveryTime %}
7.  td--{{totalDeliveryTime}}
8.{% set countDeliveries = countDeliveries + 1 %}
9.  tc--{{countDeliveries}}
10.{% endfor %}
11.  result--{{ totalDeliveryTime / countDeliveries }}
</code></pre>

<p>Result:
to--15--
td--1
tc--1 </p>

<p>to--30--
td--2
tc--2</p>

<p>to--60--
td--3
tc--3</p>

<p>to--60--
td--4
tc--4</p>

<p>result--1</p>

<p>Its a simple average calculating formula. I want to add my all 'orderDeliveryTime' in 'totalDeliveryTime' through line 6. As in line 5, it returns correct values of orderDeliveryTime which is in 'to' you can see in Result but when we add in totalDeliveryTime through line 6 it return 'td' values (please look 'td' in Result). I think it should 15+30+60+60 (addition of all orderDeliveryTime).</p>

<p>Beside this I also used </p>

<pre><code>{% set ratingsSum = 0 %}
{% if pastOrders | length %}
{% for order in pastOrders %}
{% set ratingsSum = ratingsSum + order.orderDeliveryTime %}
{% endfor %}
{% set ratingAverage = ratingsSum / pastOrders | length %}
AverageTime:{{ratingAverage}}
{% endif %}
</code></pre>

<p><a href="https://cliveportman.co.uk/code/calculate-average-of-numbers-in-craft-cms-using-twig" rel="nofollow noreferrer">https://cliveportman.co.uk/code/calculate-average-of-numbers-in-craft-cms-using-twig</a>
But it also returns the same result which is 1.</p>

CC BY-SA 3.0
2017-03-22T19:38:11.090Z
What is the actual question? I just see a bunch of code with virtually no explanation.
Lindsey D
CC BY-SA 3.0
2017-03-23T20:48:19.680Z
What type of variable is ``order.orderDeliveryTime``? Somehow ``order.orderDeliveryTime`` is returning 1 instead of the actual number when Twig is doing addition.    If you ``dump(order.orderDeliveryTime)`` what do you see? Not sure if casting it would work? eg ``totalDeliveryTime + order.orderDeliveryTime|number_format``
RitterKnight
CC BY-SA 3.0
2017-03-24T07:11:33.553Z
Actually orderDeliveryTime Getting the values from dropdown.
Mohit Prajapati
<templating><plugin-craftcommerce>
Simple average calculating formula
# Simple average calculating formula

(Asked by: username_0 on 2017-03-22)

1.{% set totalDeliveryTime = 0 %}
2.{% set countDeliveries = 0 %}
3.{% set averageDeliveryTime = 0 %}    
4.{% for order in pastOrders %}
5. to--{{order.orderDeliveryTime}}--
6.{% set totalDeliveryTime = totalDeliveryTime + order.orderDeliveryTime %}
7.  td--{{totalDeliveryTime}}
8.{% set countDeliveries = countDeliveries + 1 %}
9.  tc--{{countDeliveries}}
10.{% endfor %}
11.  result--{{ totalDeliveryTime / countDeliveries }}

Result:
to--15--
td--1
tc--1 
to--30--
td--2
tc--2
to--60--
td--3
tc--3
to--60--
td--4
tc--4
result--1
Its a simple average calculating formula. I want to add my all 'orderDeliveryTime' in 'totalDeliveryTime' through line 6. As in line 5, it returns correct values of orderDeliveryTime which is in 'to' you can see in Result but when we add in totalDeliveryTime through line 6 it return 'td' values (please look 'td' in Result). I think it should 15+30+60+60 (addition of all orderDeliveryTime).
Beside this I also used 
{% set ratingsSum = 0 %}
{% if pastOrders | length %}
{% for order in pastOrders %}
{% set ratingsSum = ratingsSum + order.orderDeliveryTime %}
{% endfor %}
{% set ratingAverage = ratingsSum / pastOrders | length %}
AverageTime:{{ratingAverage}}
{% endif %}

https://cliveportman.co.uk/code/calculate-average-of-numbers-in-craft-cms-using-twig
But it also returns the same result which is 1.

**Tags:** templating, plugin-craftcommerce

> **Comments:**
> **username_1**: What is the actual question? I just see a bunch of code with virtually no explanation.  
> **username_2**: What type of variable is ``order.orderDeliveryTime``? Somehow ``order.orderDeliveryTime`` is returning 1 instead of the actual number when Twig is doing addition.    If you ``dump(order.orderDeliveryTime)`` what do you see? Not sure if casting it would work? eg ``totalDeliveryTime + order.orderDeliveryTime|number_format``  
> **username_0**: Actually orderDeliveryTime Getting the values from dropdown.  

---

## 1 answers

### Answer by username_3 on 2017-03-24. Score: 1 | Accepted answer

You can try the following code
{% set ratingsSum = 0 %}
{% for order in pastOrders %}
    {% set ratingsSum = ratingsSum + order.orderDeliveryTime.value %}
{% endfor %}

{% set ratingAverage = ratingsSum / pastOrders | length %}
AverageTime:{{ratingAverage}}

Difference between yours and my code is i have added .value with order.orderDeliveryTime


---

