Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If you want to round a number down you can use the int() function to cut off the decimal point (i.e. 2.2 -> 2; 2.7 -> 2)

int(#form/data/value) 

 

Round a number up to the nearest integer

Use the following calculation to always round a number up (i.e. 2.2 -> 3; 2.7 -> 3)

If( int(#form/data/value) < #form/data/value, int(data#form/value) + 1, int(#form/data/value))

 

This compares whether the decimal form of the lowest integer is smaller than the current value, and if so, rounds up, and otherwise truncates the value. 

...

As of CommCare 2.19, the "round" function is available. Simply write round(#form/data/value). Read more here.
You can also use the int() function in combination with multiplying the input by 2, adding 1, and then dividing by 2 (i.e. 2.2 -> 2; 2.7 -> 3)
int ( ( #form/data/value*2 +1 ) div 2 )
To round to a different number of decimal places, appropriately adjust the constant '2' in the preceding calculation. The constant should be twice the inverse of the desired precision. For example, for a precision of 0.1, constant = 1 / precision * 2 = 1 / 0.1 * 2 = 20. Consequently use int ( ( #form/data/value*20 +1 ) div 20 ).

Round a number to a certain decimal place

You can use the same principles to round to the nearest .1, .01, .001, etc. with the following formula:
Round to nearest decimal:  round(#form/data/value*10) div 10
To round to two decimals change the 10's in the formula above to 100. To three decimals change them to 1000, etc.