Last updated 14 December 2016. Created on 6 May 2015.
Edited by yidanzhu, k8lyn6, draenen. Log in to edit this page.

The Math Field module defines a new field type to dynamically calculate values on entity forms. Math Field is an alternative to Computed Field that does not require the use of PHP Filter.


Download, install, and enable the cTools and Math Field modules.

Field configuration

Math Field creates a new field type that may be added to content types or any other fieldable entity, such as users or taxonomy_terms. To add a math field, navigate to the "Manage fields" page of a content type and add a new "Math Expression" field. Math Field provides two widget types: text field and read-only.

The text field widget will create a user editable input element that automatically populates with the result of the math expression. The user will be able to overwrite this result with a different value. The read-only widget will create un-editable markup to display the result of the math expression.

The math expression is used to evaluate in the "Math Expression" field setting. For example: 2 + 2 or sqrt(25). You can use any of the basic operators (+-*/^) as well as advanced functions such as sqrt(), sin(), or ln(). You can find a full list of available functions on the "Supported math functions" page.

The following settings additionally apply to the math expression result.

  • Decimal marker: The character to mark the decimal in the result. Options are Decimal Point or Comma.
  • Precision: The total number of digits to store in the database, including to the right of the decimal.
  • Scale: The number of digits displayed to the right of the decimal.

Other standard fields settings are also available including "required field", "help text", "prefix", and "suffix".

After the field is saved, you will see the result of the expression on the node/add form for your content type.

Advanced math expressions using other fields

You can include values from other fields in math expressions. The math field result is dynamically updated via Ajax on the form as soon as the necessary fields are populated. If JavaScript is disabled, the math field is evaluated when the form is submitted. By default, Math Field supports using numeric (integer or float) fields and lists in math expressions. Other modules may provide support by implementing hook_mathfield_get_token() and hook_mathfield_get_token_value().

Field tokens may may be added to the form using the token format [$field_name:$delta:$column] where $field_name is the machine name of the field (e.g., field_a), $delta is the position of a multivalue field ($delta starts at 0), and $column is the name of the column to use for multicolumn fields. $delta and/or $column may be omitted for single value and column fields respectively.

On the node/add form, the math expression is evaluated as soon as the dependent fields are populated. The result of the expression is saved to the database.

Looking for support? Visit the forums, or join #drupal-support in IRC.