# How to use math functions in price formulas

The price formula field type in WooCommerce Product Options allows store owners to create custom calculations for product prices based on information that the customer provides. Price formulas can be very simple or very complex! This article provides details of how to use advanced mathematical formulas in your price formulas.

## Available variables

You can use price formulas to calculate the final price of a product based on variables such as:

- Numbers entered by the customer. These must come from number fields which are in the same product options group as the price formula field.
- The base price of the current product
`[product_price]`

.

## Mathematical operators

The price formula field type supports common mathematical operators, such as `+`

, `-`

, `*`

and `/`

. In addition, it can parse all the mathematical functions included in the Javascript `Math`

object.

A complete list of those functions can be found on the Javascript Math page of the MDN Web Docs.Â Those functions can be used by entering their name (without the `Math.`

prefix itself) in the formula. Below, we will explore some practical examples to help you to apply this to different use cases.

### How to limit prices

The functions `min`

and `max`

accept two parameters and return the parameter that has the minimum and maximum value, respectively.

#### Example: max

A store sells made-to-measure curtainsÂ (like in this demo product). The product costs **$50 per square meter**. Customers are asked to enter the **width** and **height** they need â€“ in centimeters â€“ but they will incur a minimum charge of **$50** regardless of the total size being ordered.

You can achieve this with the following formula:

`max( 50, ( [width] / 100 ) * ( [height] / 100 ) * [product_price] )`

In the formula above, the function will return `[width] * [height] * [product_price]`

if it is more than `50`

. Otherwise, it will always return `50`

, no matter how small the values for `[width]`

and `[height]`

are.

For example, when **width** is **100** and **height** is **50**, the formula is calculated as follows:

`max( 50, ( 100 / 100 ) * ( 50 / 100 ) * 50 ) = max( 50, 1 * 0.5 * 50 ) = max( 50, 25 ) = 50`

### How to round prices

You can round the value of a calculated price by using function among `round`

, `trunc`

, `floor`

and `ceil`

, with the following specifications:

`round`

approximates the value to the closest integer (e.g`round( 3.37 )`

returns`3`

;`round( 5.98 )`

returns 6).`floor`

returns the closest integer smaller than the input value (e.g`floor( 3.37 )`

returns`3`

;`floor( 5.98 )`

returns`5`

).`trunc`

truncates the decimal portion of a number (e.g.`trunc( 3.37 )`

returns 3;Â`floor( 5.98 )`

returns`5`

). With positive numbers,`trunc`

and`floor`

both return the same value.`ceil`

Â returns the closest integer greater than the input value (e.g`floor( 3.37 )`

returns`4`

;`floor( 5.98 )`

returns`6`

).

#### Example: ceil

A store sells pet food. Dog food is **$28.15 per kg**. Customers can enter the required **weight** with **0.1 kg precision**. The shop owner wants prices to be calculated to the whole dollar, approximating by excess. The appropriate formula is:

`ceil( [weight] * [product_price] )`

For example, if **2.5** is entered as **weight**, the formula is calculated as follows:

`ceil( 2.5 * 28.15 ) = ceil( 70.375 ) = 71`

#### Example: round

A website provides housekeeping services and allows customers to book cleaners in **1-hour slots**. The cost for **1 hour** is **$19.85** and the site owner wants to round the price calculation in **$5 increments** so that **$19.85** returns **$20** and **$357.30** returns **$355**. The appropriate formula is:

`5 * round( [hours] * [product_price] / 5 )`

For example, if a customer requests **18** hours, the formula is calculated as follows:

`5 * round( 18 * 19.85 / 5 ) = 5 * round( 71.46 ) = 5 * 71 = 355`

### Logical or conditional clauses

Price formulas are limited to mathematical operations and do not understand logical structures. Therefore, expressions like `if ( [width] < 200 ) then [product_price] else 1.25 * [product_price]`

**cannot be used** in a price formula. Instead, you can use mathematical functions and operators to transform logical clauses into numbers.

#### Equivalence between logical and mathematical constants and operators

The basic equivalencies between logic and maths are:

`true`

and`false`

are equivalent to`1`

and`0`

, respectively`a AND b`

is equivalent to a multiplication`a * b`

`a OR b`

is equivalent to an addition`a + b`

`a > b`

can be written as`max( 0, sign( a - b ) )`

`a < b`

can be written as`max( 0, sign( b - a ) )`

Let's see how this can help us write conditional structures in the context of price formulas. Please consider the following example. A store sells shipping services and doubles its rates for oversized items. Users are invited to enter the **Width**, **Height** and **Depth** of the package they need to ship. The store considers a package oversized **if the sum of the three lengths is more than 100 centimeters**. In a regular coding language, one would write:

**if** [Width] + [Height] + [Depth] > 100 **then** 2 * [product_price] **else** [product_price]

but we cannot write this in a price formula. What we would write is:

`[product_price] * (1 + max( 0, sign( [Width] + [Height] + [Depth] - 100 ) ) )`

The function `sign`

returns **1**, **-1** or **0** depending on the passed parameter being positive, negative or zero. Since `max`

forces the result to be greater than 0, what the function does is return **1** if **[Width] + [Height] + [Depth]** is greater than 100 and 0 otherwise.

For example, if a customer wants to ship a package that is **45 cm** wide, **40 cm** high and **20 cm** deep, the formula is calculated as follows:

`[product_price] * ( 1 + max( 0, sign( 45 + 40 + 20 - 100 ) ) ) =`

[product_price] * ( 1 + max( 0, sign( 5 ) ) ) =

[product_price] * ( 1 + max( 0, 1 ) ) =

[product_price] * ( 1 + 1 ) =

[product_price] * 2

which is exactly what the store requires.

Let's see another example.