Displaying floating point value with specific precision

We’d like to display a floating point value (e.g. 34.942), using a
precision of 1 (e.g. 34.9). We did not find an xpath function documented
here
https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-number
to allow us to do that. Is there a way to do this? (e.g. something like
int(/data/value*10)/10) Thanks, Ray

Hi Ray,

See this section
https://confluence.dimagi.com/display/commcarepublic/Common+Logic+and+Calculations#CommonLogicandCalculations-Roundingnumbers
on the help page. Specifically:

Round a number up from .5, otherwise down
You can 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 ( ( /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 ( (
/data/value*20 +1 ) div 20 ).

Hope this helps,
Kriti

··· On Wed, Nov 26, 2014 at 10:51 PM, Ray Brunsting wrote:

We’d like to display a floating point value (e.g. 34.942), using a
precision of 1 (e.g. 34.9). We did not find an xpath function documented
here
https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-number
to allow us to do that. Is there a way to do this? (e.g. something like
int(/data/value*10)/10) Thanks, Ray


You received this message because you are subscribed to the Google Groups
"commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to commcare-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thanks Kriti. We’ll give it a try. Ray

··· On Wed, Nov 26, 2014 at 12:45 PM, Kriti Mehrotra wrote:

Hi Ray,

See this section
https://confluence.dimagi.com/display/commcarepublic/Common+Logic+and+Calculations#CommonLogicandCalculations-Roundingnumbers
on the help page. Specifically:

Round a number up from .5, otherwise down
You can 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 ( ( /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 ( (
/data/value*20 +1 ) div 20 ).

Hope this helps,
Kriti

On Wed, Nov 26, 2014 at 10:51 PM, Ray Brunsting ray@tula.org wrote:

We’d like to display a floating point value (e.g. 34.942), using a
precision of 1 (e.g. 34.9). We did not find an xpath function documented
here
https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-number
to allow us to do that. Is there a way to do this? (e.g. something like
int(/data/value*10)/10) Thanks, Ray


You received this message because you are subscribed to the Google Groups
"commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to commcare-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


You received this message because you are subscribed to the Google Groups
"commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to commcare-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Ray Brunsting, CTO, Tula Foundation / www.tula.org