Answers.Body: <p>As always, for anything non-trivial, you're probably better off writing a Twig extension to abstract your logic to PHP...</p>
  <p>That said, for &quot;quick and dirty&quot; calculations, Money objects have an <code>amount</code> property that will give you the value (but as a string, and in cents). It's pretty easy to parse that to something more useful though:</p>
  <pre><code>{# get the value of a money field as a float #}
  {% set value = moneyField.amount | float / 100 %}
  </code></pre>
  <p>You can then do whatever math operations you need to do, before piping through the <code>currency</code> filter for formatting:</p>
  <pre><code>{{ value | currency('USD') }}
  </code></pre>
  <p>You can also use the Money object's <code>currency</code> property if you need to dynamically determine the currency from the field:</p>
  <pre><code>{{ value | currency(moneyField.currency) }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-04-26T12:33:22.497Z
Answers.LastActivityDate: 2023-04-27T03:01:33.683Z
Answers.LastEditDate: 2023-04-27T03:01:33.683Z
Answers.UserDisplayName: Tom Davies
Body: <p>Maybe I'm overlooking something, but can't find a solution on the interwebs..</p>
  <p>I have 2 (money) fields: product.price and product.oldPrice</p>
  <p>When I try to calculate the discount like this (I'm aware that this simple approach shouldn't work):</p>
  <pre><code>{% set discount = product.price / product.oldPrice * 100 %}
  </code></pre>
  <p>I get this error message:
  <em><strong>Unsupported operand types: Money\Money / Money\Money</strong></em></p>
  <p>And when I try calling it as a 'float':</p>
  <pre><code>{% set discount = product.price|number_format(2) / product.oldPrice|number_format(2) * 100 %} 
  </code></pre>
  <p>I get this error message:
  <em><strong>Object of class Money\Money could not be converted to float</strong></em></p>
  <p>The only solution I can think of is changing the field type in '<em><strong>Float</strong></em>', but that would make things more difficult.</p>
  <p>I also tried Martin Spains solution, who suggested to call the value of the objects like this:</p>
  <pre><code>{% set discount = product.price.value / product.oldPrice.value * 100 %}
  </code></pre>
  <p>But then I get this error:</p>
  <p><em>Neither the property &quot;value&quot; nor one of the methods &quot;value()&quot;, &quot;getvalue()&quot;/&quot;isvalue()&quot;/&quot;hasvalue()&quot; or &quot;__call()&quot; exist and have public access in class &quot;Money\Money&quot;.</em></p>
  <p>I hope someone has a solution for me</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-02-15T14:42:58.260Z
Comments.Text: I've not used Money fields before, but I wonder if you might need to access the value of the field like this: `product.price.value` to get the value itself rather than the field model.
Comments.UserDisplayName: Martin Spain
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-03-07T09:41:56.327Z
Comments.Text: Thanks @MartinSpain! I gave it a try, but unfortunately without any luck. Check my updated post
Comments.UserDisplayName: Hielke
Tags: <fieldtypes>
Title: How to calculate with money fields? Like extracting discount percentage
ThreadText: # How to calculate with money fields? Like extracting discount percentage
  
  (Asked by: username_0 on 2023-02-12)
  
  Maybe I'm overlooking something, but can't find a solution on the interwebs..
  
  I have 2 (money) fields: product.price and product.oldPrice
  
  When I try to calculate the discount like this (I'm aware that this simple approach shouldn't work):
  
  ```
  {% set discount = product.price / product.oldPrice * 100 %}
  
  ```
  
  I get this error message:
  ***Unsupported operand types: Money\Money / Money\Money***
  
  And when I try calling it as a 'float':
  
  ```
  {% set discount = product.price|number_format(2) / product.oldPrice|number_format(2) * 100 %} 
  
  ```
  
  I get this error message:
  ***Object of class Money\Money could not be converted to float***
  
  The only solution I can think of is changing the field type in '***Float***', but that would make things more difficult.
  
  I also tried username_1 Spains solution, who suggested to call the value of the objects like this:
  
  ```
  {% set discount = product.price.value / product.oldPrice.value * 100 %}
  
  ```
  
  But then I get this error:
  
  *Neither the property "value" nor one of the methods "value()", "getvalue()"/"isvalue()"/"hasvalue()" or "\_\_call()" exist and have public access in class "Money\Money".*
  
  I hope someone has a solution for me
  
  **Tags:** fieldtypes
  
  > **Comments:**
  > **username_1**: I've not used Money fields before, but I wonder if you might need to access the value of the field like this: `product.price.value` to get the value itself rather than the field model.  
  > **username_0**: Thanks @username_1! I gave it a try, but unfortunately without any luck. Check my updated post  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2023-04-26. Score: 2
  
  As always, for anything non-trivial, you're probably better off writing a Twig extension to abstract your logic to PHP...
  
  That said, for "quick and dirty" calculations, Money objects have an `amount` property that will give you the value (but as a string, and in cents). It's pretty easy to parse that to something more useful though:
  
  ```
  {# get the value of a money field as a float #}
  {% set value = moneyField.amount | float / 100 %}
  
  ```
  
  You can then do whatever math operations you need to do, before piping through the `currency` filter for formatting:
  
  ```
  {{ value | currency('USD') }}
  
  ```
  
  You can also use the Money object's `currency` property if you need to dynamically determine the currency from the field:
  
  ```
  {{ value | currency(moneyField.currency) }}
  
  ```
  
  
  ---
  
  