# Work with numbers and math functions

Use rule boxes to work with numbers and math functions in Productsup.

## Introduction

Use rule boxes to work with numbers and math functions from Data View in the platform, such as creating discounts, using formulas to calculate Return on Advertising Spend (ROAS), or calculating and managing your numeric data. Rule boxes also let you create random numbers.

### Note

When working with numbers, ensure they are valid numbers, either integers or floats and don't contain any text elements, for example, commas. Consider using the Make Valid Price rule box to normalize the numbers. See Validate prices and convert strings to integers.

## Generate random numbers

To create a random number in a column, you can add the Generate Random Number rule box.

### Generate MD5 Hash rule box

To generate a unique and encrypted MD5 hash for your column's input data, you can add the Generate MD5 Hash rule box.

### Generate Unique ID rule box

You can create a unique ID for each product using the Generate Unique ID rule box. This box generates the same ID for all products listed under your chosen column.

Alternatively, you can input columns you want the Generate Unique ID rule box to check for uniqueness when generating the ID.

Optionally, you can define columns (newline-separated) for the rule box that should group products by creating a unique ID for each group. You can also consider using the Unique ID Generator service found in Data Services to achieve the same effect.

## Adding VAT to prices

To add VAT to your prices, you can use the Add VAT rule box:

You must enter the VAT percentage in the Adds VAT to a Price field.

You can choose, to round, always round up, or round down the percentage.

## Calculate discounts

You can calculate prices based on a set discount or calculate the discount percentage between the original price and the sale price with the Discount rule box.

### Discount rule box

To calculate prices based on a set discount and when your products meet a specific condition, consider using the Discount rule box. This rule box lets you:

Define a column based on your situation and add the condition in the Mandatory Column input field.

Optionally, add a condition in the Optional Column input field.

Enter the percentage of discount you want to apply in the Discount input field.

Define what happens when a product from your current column does not contain a number.

### Note

If you want to apply a discount to all products, you can add a mandatory condition: `price > 0`

.

### Discount Percentage rule box

To calculate the discount percentage between two prices, you can use the Discount Percentage rule box. This rule box helps determine the percentage from the regular and sale price. With the Discount Percentage rule box, you can:

Define the original value by selecting the applicable column in Old Price. Then choose the column containing the desired price in New Price.

Use Round Precision to define how many decimal places you wish to calculate the percentage.

Optionally, add text before or after your percentage calculation in Text Before and Text After.

## Compare numbers from two columns

To compare numbers from two columns and assign a value based on the comparison outcome, you can use the Numeric Compare rule box.

You first select your first column for the comparison and choose the operator you want to use in the comparison drop-down menu.

Select your second column for the comparison.

Define the value you want to assign when the comparison finds a match in the handle match input field.

Determine what happens when there is no match under the handle no match input field.

## Round numbers

You can round numbers using the Round Numeric Values rule box:

Define how many decimal places you want to calculate the percentage under Round Precision.

You can decide what should occur if your current input is an invalid number in the If column value is not a drop-down menu.

## Perform mathematics calculations

### Math rule box

Using the Math rule box, you can perform mathematics calculations for a column, which involves either a static value or values from another column.

Use the Operator drop-down menu to select whether you would like to

*add*,*subtract*,*multiply*, or*divide*a value from the current column.In Column, choose a value you wish to use to perform the above operation from the drop-down list. Or, in Static Value, enter a static value as desired instead.

Define how many decimal places you want to calculate the percentage under Round Precision.

Leaving the Round Precision field empty does not affect the outcome.

You can decide what should occur if your current input is an invalid number in the If column value is not a drop-down menu.

You can select the round method.

### Column Math rule box

You can also perform calculations using multiple columns by adding the Column Math rule box.

Use the Operator drop-down menu to select whether you would like to

*add*,*subtract*,*multiply*, or*divide*a value from the current column.In Input Column 1, choose a value you wish to use to perform the above operation from the drop-down list. Or, in Input Static Value 1, enter a static value as desired instead.

In Input Column 2, choose a value you wish to use to perform the above operation from the drop-down list. Or, in Input Static Value 2, enter a static value as desired instead.

Define how many decimal places you want to calculate the percentage under Round Precision.

Leaving the Round Precision field empty does not affect the outcome.

You can decide what should occur if your current input is an invalid number in the If column value is not a drop-down menu.

You can select the round method.

### Tip

The Column Math rule box first calculates the Input Column 1 or Input Static Value 1 and the second Input Column 2 or Input Static Value 2. Once this action completes, the rule box performs a calculation between the input data and the newly calculated value.

In the following example, you add `20`

to the `price`

column before adding and returning this calculated value to the current `price`

column.

### Math Template rule box

You can define your calculations using a formula with the Math Template rule box for more flexibility. Use up to four columns in this rule box, including the current input column.

Define up to three additional columns to use for your calculations with Optional Column 1, 2, and 3.

Enter a valid Twig template for your calculation:

The Twig should begin and end with curly brackets and whitespace:

*{{ calculation }}*.You can refer to the values present in the columns as

*value*.

Define how many decimal places you want to calculate the percentage under Round Precision.

You can create complex formulas using Twig templates. The following example takes the current input value, `value`

and divides it by the value in column1. It then multiplies the result by `45`

and then adds the result to the value from `column2`

:

{{ (value / column1) * 45 + column2 }} |

See the Twig Fiddle template to create and test your formulas.

### Display numerals instead of in scientific (exponential) notation format

If your calculation results in a long number, the platform displays them in the scientific (exponential) notation instead of numerals with decimal and trailing zeros. For example, you may have `5.7E-3`

in scientific notation, but you want to display `0.000057`

instead.

You can use a Twig template to convert your calculation results into numerals only. The following example shows a breakdown of how the process works.

`{{(currentValue/1000000)|number_format(9)|trim('0', 'right')}}`

Twig performs a calculation,

*current value*divided by*1,000,000*. For example,`(currentValue/1000000)`

. For example:`57 / 1,000,000 = 0.000057`

Next, in this example, you define the format of your values by adding nine zeros (0) to the trailing end of the value -

`number_format(9)`

. Defining the format means, Twig trims or removes any trailing zeros at the end of the value up to nine places:`0.000057`

**000**Finally, tell Twig to trim zeros from the right going left -

`trim('0', 'right')`

:`0.000057`

=**000**`0.000057`

You can illustrate values in a numeric format by adding a Twig template in the *Math* or *Text Template* rule box. See Math Template rule box for more information.

See the Twig Fiddle template to create and test your formulas.

### In-row Math rule box

You can use the In-Row Math rule box to add or multiply the values in one column per product.

For example:

Input: `1,4,6`

Operator: `add`

Output: `11`

Enter a character to separate strings or cell values in Delimiter. Common delimiters are commas (

`,`

), semicolons (`;`

), quotes (`"`

,`'`

), braces (`{}`

), pipes (`|`

), or slashes (`/ \`

).In Operator, define how to calculate the values by choosing Add or Multiply from the drop-down menu.