Display a calculation to 2 decimal places

Hi,

Im trying to display the answer to a BMI calculation, but I only want it to
2 decimal places, otherwise the long list of decimals can be confusing.

ie. “Mother’s BMI is <output…>”

I can only find on the help pages how to round to a whole number. Is there
a way of doing this for 2 decimal places?

Thanks

Natasha

Hi Natasha,

Please take a look at the final paragraph of the Rounding Numbers
https://confluence.dimagi.com/display/commcarepublic/Common+Logic+and+Calculations#CommonLogicandCalculations-Roundingnumbers
section
of the logic and calculations page.

Thanks,
Will

··· On Mon, Dec 8, 2014 at 3:02 AM, Natasha Lelijveld < natasha.lelijveld@gmail.com> wrote:

Hi,

Im trying to display the answer to a BMI calculation, but I only want it
to 2 decimal places, otherwise the long list of decimals can be confusing.

ie. “Mother’s BMI is <output…>”

I can only find on the help pages how to round to a whole number. Is there
a way of doing this for 2 decimal places?

Thanks

Natasha


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.

Hi Will,

thanks.

I was trying to follow those instructions :

For example, for a precision of 0.1, constant = 1 / precision * 2 = 1 / 0.1

  • 2 = 20. Consequently use int ( ( /data/value20 +1 ) div 20 )*.

But I thought because the logic has “int” at the start, doesn’t this mean
it will just come back with a whole number?

Hi Natasha,

I believe that formula is slightly wrong. The div 20 should be outside the
int() expression. It should be:

int ( /data/value20 +1 ) div 20 *

I updated the help site accordingly.

Regards,
Nick

Nick Nestle
Project Manager | Dimagi South Africa
+27 79 439 6081

··· On Tue, Dec 9, 2014 at 9:58 AM, Natasha Lelijveld < natasha.lelijveld@gmail.com> wrote:

Hi Will,

thanks.

I was trying to follow those instructions :

For example, for a precision of 0.1, constant = 1 / precision * 2 = 1 /
0.1 * 2 = 20. Consequently use int ( ( /data/value20 +1 ) div 20 )*.

But I thought because the logic has “int” at the start, doesn’t this mean
it will just come back with a whole number?


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.

Hi Natasha,

sorry, to correct myself, that example was to round to the nearest .5. I
added a section for rounding to certain decimal places (and pasting below
here):

··· =============

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: * int(/data/value10 + .5) 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.

Nick Nestle
Project Manager | Dimagi South Africa
+27 79 439 6081

On Tue, Dec 9, 2014 at 10:09 AM, Nick Nestle nnestle@dimagi.com wrote:

Hi Natasha,

I believe that formula is slightly wrong. The div 20 should be outside
the int() expression. It should be:

int ( /data/value20 +1 ) div 20 *

I updated the help site accordingly.

Regards,
Nick

Nick Nestle
Project Manager | Dimagi South Africa
+27 79 439 6081

On Tue, Dec 9, 2014 at 9:58 AM, Natasha Lelijveld < natasha.lelijveld@gmail.com> wrote:

Hi Will,

thanks.

I was trying to follow those instructions :

For example, for a precision of 0.1, constant = 1 / precision * 2 = 1 /
0.1 * 2 = 20. Consequently use int ( ( /data/value20 +1 ) div 20 )*.

But I thought because the logic has “int” at the start, doesn’t this mean
it will just come back with a whole number?


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.

Hi Nick,

Thank you - that’s working perfectly now

Best wishes

··· On Tuesday, 9 December 2014 10:24:54 UTC+2, Nick Nestle wrote: > > Hi Natasha, > > sorry, to correct myself, that example was to round to the nearest .5. I > added a section for rounding to certain decimal places (and pasting below > here): > > > ============= > > *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: * int(/data/value*10 + .5) 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. > > Nick Nestle > Project Manager | Dimagi South Africa > +27 79 439 6081 > > > On Tue, Dec 9, 2014 at 10:09 AM, Nick Nestle <nne...@dimagi.com > wrote: > >> Hi Natasha, >> >> I believe that formula is slightly wrong. The div 20 should be outside >> the int() expression. It should be: >> >> *int ( /data/value*20 +1 ) div 20 * >> >> I updated the help site accordingly. >> >> Regards, >> Nick >> >> Nick Nestle >> Project Manager | Dimagi South Africa >> +27 79 439 6081 >> >> >> On Tue, Dec 9, 2014 at 9:58 AM, Natasha Lelijveld <natasha....@gmail.com > wrote: >> >>> Hi Will, >>> >>> thanks. >>> >>> I was trying to follow those instructions : >>> >>> 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 )*. >>> >>> >>> But I thought because the logic has "int" at the start, doesn't this >>> mean it will just come back with a whole number? >>> >>> -- >>> 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-user...@googlegroups.com . >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> >