# How to do simple subtraction in Computed Field?

I'm trying to calculate the price difference between old price and new price, and if the price has dropped to display by how much, both Nominally and in Percent.

So, basically these are 2 simple formulas that should look like this:

NOMINAL PRICE DROP = field_price_original - field_price_new

PERCENTAGE PRICE DROP = ( (field_price_new - field_price_original) / field_price_original ) * 100

I tried setting this up through Rules (unsuccessfully), but it seems that doing it through Computed Field will not be so heavy on the server. Using Computing Field I have the following formula:

\$entity_field['value'] = array_pop(array_pop(field_get_items(\$entity_type, \$entity, 'field_price_original'))) - array_pop(array_pop(field_get_items(\$entity_type, \$entity, 'field_price_new')));

The number is set as float, 10.2. But for some reason it doesn't work.

Can someone please help and provide the correct syntax for Computed Field?

As per the documentation of computation field module to calculate the difference of two fields you need to use the PEAR:

``````\$path = '/pathToPearsParentDirectory/pear/PEAR';
set_include_path(get_include_path() . PATH_SEPARATOR . \$path);

require_once 'PEAR.php';
include_once "Text/Diff.php";
include_once "Text/Diff/Renderer.php";
include_once "Text/Diff/Renderer/inline.php";

\$diff = &new Text_Diff('auto', array(array(\$node->field_nameOfOneFieldToCompare['value']), array(\$node->field_nameOfOtherFieldToCompare['value']))); \$renderer
= &new Text_Diff_Renderer_inline();

\$node_field['value'] = \$renderer->render(\$diff);
``````

``````\$entity_field['value'] = \$entity->field_price[LANGUAGE_NONE]['value'] - \$entity->field_cost[LANGUAGE_NONE]['value'];