Calculated Fields Form Blog

Tips and cases of use for a successful WordPress website with calculated forms.

Blog / Rounding Numbers

Rounding Numbers

The post describes all the rounding operations available in the plugin and its alternatives.


There are recurring questions from users, both in our support system and in the WordPress forum such as:

How do you round up the results of the equation to the nearest whole number?
Or, how to display the result with only two decimals?
Or, how to round up the result to the next multiple of a thousand?

And other similar questions.

The rounding operations

The plugin includes multiple rounding operations, listed and described below:

ROUND(X) returns the integer number nearest or equal to X.

Ex. ROUND(3.46) returns 3

ROUND(3.64) returns 4

ROUND(6.5) returns 7

ROUND(-6.5) returns -6

ROUND(10) returns 10

Using the ROUND operation in an equation. Assuming the equation associated with the calculated field is fieldname1*fieldname2, and you want the result as the nearest integer number, edit the equation as ROUND(fieldname1*fieldname2)

The ROUND operation accepts a second parameter to allow rounding the result to the nearest multiple of the second parameter.

Ex. ROUND(3.46, 0.1) returns 3.5

ROUND(3.64, 0.1) returns 3.6

FLOOR(X) returns an integer number less than or equal to X.

Ex. FLOOR(4.12) returns 4

FLOOR(4.99) returns 4

FLOOR(12) returns 12

FLOOR(-2.1) returns -3

Using the FLOOR operation in an equation. Assuming the value of fieldname1 is a decimal representing the number of minutes, and you want to transform it as hours:minutes, a possible equation would be:

FLOOR(fieldname1/60)+':'+(fieldname1%60)

Note: in javascript the % operator, returns the rest of division.

The FLOOR operation accepts too a second parameter to allow rounding the result to the previous multiple of the second parameter.

Ex. FLOOR(4.1265, 0.01) returns 4.12

FLOOR(4.1265, 0.1) returns 4.1

CEIL(X) returns an integer number greater than or equal to X.

Ex. CEIL(6.0001) returns 7

CEIL(8.999) returns 9

CEIL(3) returns 3

CEIL(-2.1) returns -2

Using the CEIL operation in an equation.

Assuming you manage a transportation business, and you have implemented a form where the trucks' drivers enter the origin and destination addresses, and it calculates the distance in miles:

DISTANCE(fieldname1, fieldname2, 'mi', 'DRIVING')

Note: the DISTANCE operation is distributed with the Developer and Platinum versions of the plugin.

However, to calculate the travel's duration, knowing the maximum distance a truck can safely travel per day is 320 miles (this number is hypothetical, only to describe the use of the CEIL operation), the previous equation should be edited as follows:

CEIL(DISTANCE(fieldname1, fieldname2, 'mi', 'DRIVING')/320)

The CEIL operation accepts a second parameter for rounding the result to the next multiple of the second parameter.

Ex. CEIL(4.1265, 0.01) returns 4.13

CEIL(4.1265, 0.1) returns 4.2

PREC(X, Y) returns the number X rounded to Y decimals. The PREC operation is widely recommended for currency results.

Ex. PREC(3.7649,2) returns 3.76

PREC(3.7649,3) returns 3.765

Using the PREC operation in an equation.

If the price of one bean Kg is $3.12, and the user enters the kilograms of beans to buy into the fieldname1. The equation can be implemented as follows:

PREC(fieldname1*3.12, 2)