Round() / Int() etc. in commcare

Hi all,
I’m wondering about how to do rounding in commcare. We are trying to
calculate the number of weeks in a pregnancy based on lmp_date and today’s
date. Its a simple division and round issue.

ODK’s javarosa source now supports “int()”
(see http://opendatakit.org/help/form-design/binding/ /
https://bitbucket.org/m.sundt/javarosa/src/62409ae3b803/core/src/org/javarosa/xpath/expr/XPathFuncExpr.java#cl-129
if you need details).

What is the commcare equivalent / is there one? (We tried round, but it
didn’t work).
cheers,
–prabhas

Hey Prabhas,

We do support “int()”. Check out the lastest list of all xpath functions
available in CommCare:
https://wiki.commcarehq.org/display/commcarepublic/List+of+xpath+functions

Amelia

··· On Tue, Oct 9, 2012 at 11:35 AM, Prabhas Pokharel < prabhas.pokharel@gmail.com> wrote:

Hi all,
I’m wondering about how to do rounding in commcare. We are trying to
calculate the number of weeks in a pregnancy based on lmp_date and today’s
date. Its a simple division and round issue.

ODK’s javarosa source now supports “int()” (see
http://opendatakit.org/help/form-design/binding/ /
https://bitbucket.org/m.sundt/javarosa/src/62409ae3b803/core/src/org/javarosa/xpath/expr/XPathFuncExpr.java#cl-129if you need details).

What is the commcare equivalent / is there one? (We tried round, but it
didn’t work).
cheers,
–prabhas

Does the int() support date calculations? I’m trying to subtract two dates
and divide by 7 to get the rounded off number of gestational weeks, so the
command looks like this: int((today - lmp_date) div 7), but nothing is
showing up for this field. I removed the div 7, just to see if int would
work with subtracting two dates, and still nothing shows up. Any thoughts?

··· On Tuesday, October 9, 2012 11:47:49 PM UTC+5:30, Amelia Sagoff wrote: > > Hey Prabhas, > > We do support "int()". Check out the lastest list of all xpath functions > available in CommCare: > https://wiki.commcarehq.org/display/commcarepublic/List+of+xpath+functions > > Amelia > > On Tue, Oct 9, 2012 at 11:35 AM, Prabhas Pokharel <prabhas....@gmail.com wrote: > >> Hi all, >> I'm wondering about how to do rounding in commcare. We are trying to >> calculate the number of weeks in a pregnancy based on lmp_date and today's >> date. Its a simple division and round issue. >> >> ODK's javarosa source now supports "int()" (see >> http://opendatakit.org/help/form-design/binding/ / >> https://bitbucket.org/m.sundt/javarosa/src/62409ae3b803/core/src/org/javarosa/xpath/expr/XPathFuncExpr.java#cl-129if you need details). >> >> What is the commcare equivalent / is there one? (We tried round, but it >> didn't work). >> cheers, >> --prabhas >> > >

Carolyn,

Is “today” defined in your app currently, or are you trying to use the
"today" function? the function would be used as

int((today() - lmp_date) div 7)

Alternatively, depending on what version of the engine you’re running,
sometimes the app has issues with maintaining the datatype for computed
dates, so you can try wrapping any referenced node with the date() cast,
IE: (Assuming both “today” and “lmp_date” are nodes)

int( (date(today) - date(lmp_date)) div 7)

No need to explicitly turn a date into an int, if you subtract two dates,
the result will always be an integer.

Can you try these changes and see if they address your issue?

-Clayton

··· On Wed, Oct 10, 2012 at 2:32 AM, Carolyn Gulas wrote:

Does the int() support date calculations? I’m trying to subtract two
dates and divide by 7 to get the rounded off number of gestational weeks,
so the command looks like this: int((today - lmp_date) div 7), but nothing
is showing up for this field. I removed the div 7, just to see if int
would work with subtracting two dates, and still nothing shows up. Any
thoughts?

On Tuesday, October 9, 2012 11:47:49 PM UTC+5:30, Amelia Sagoff wrote:

Hey Prabhas,

We do support “int()”. Check out the lastest list of all xpath functions
available in CommCare: https://wiki.commcarehq.org/display/
commcarepublic/List+of+xpath+**functionshttps://wiki.commcarehq.org/display/commcarepublic/List+of+xpath+functions

Amelia

On Tue, Oct 9, 2012 at 11:35 AM, Prabhas Pokharel prabhas....@gmail.comwrote:

Hi all,
I’m wondering about how to do rounding in commcare. We are trying to
calculate the number of weeks in a pregnancy based on lmp_date and today’s
date. Its a simple division and round issue.

ODK’s javarosa source now supports “int()” (see http://opendatakit.org/*
*help/form-design/binding/http://opendatakit.org/help/form-design/binding//
https://bitbucket.org/m.sundt/javarosa/src/62409ae3b803/
core/src/org/javarosa/xpath/**expr/XPathFuncExpr.java#cl-129https://bitbucket.org/m.sundt/javarosa/src/62409ae3b803/core/src/org/javarosa/xpath/expr/XPathFuncExpr.java#cl-129if you need details).

What is the commcare equivalent / is there one? (We tried round, but it
didn’t work).
cheers,
–prabhas

Hi Carolyn,

You can also find some examples of commonly used calculations (including
age calculations) on our wiki here:
https://wiki.commcarehq.org/display/commcarepublic/Common+Logic+and+Calculations

··· On Wed, Oct 10, 2012 at 11:16 AM, Clayton Sims wrote:

Carolyn,

Is “today” defined in your app currently, or are you trying to use the
"today" function? the function would be used as

int((today() - lmp_date) div 7)

Alternatively, depending on what version of the engine you’re running,
sometimes the app has issues with maintaining the datatype for computed
dates, so you can try wrapping any referenced node with the date() cast,
IE: (Assuming both “today” and “lmp_date” are nodes)

int( (date(today) - date(lmp_date)) div 7)

No need to explicitly turn a date into an int, if you subtract two dates,
the result will always be an integer.

Can you try these changes and see if they address your issue?

-Clayton

On Wed, Oct 10, 2012 at 2:32 AM, Carolyn Gulas cagulas@gmail.com wrote:

Does the int() support date calculations? I’m trying to subtract two
dates and divide by 7 to get the rounded off number of gestational weeks,
so the command looks like this: int((today - lmp_date) div 7), but nothing
is showing up for this field. I removed the div 7, just to see if int
would work with subtracting two dates, and still nothing shows up. Any
thoughts?

On Tuesday, October 9, 2012 11:47:49 PM UTC+5:30, Amelia Sagoff wrote:

Hey Prabhas,

We do support “int()”. Check out the lastest list of all xpath
functions available in CommCare: https://wiki.*commcarehq.org/display/
*commcarepublic/List+of+xpath+**functionshttps://wiki.commcarehq.org/display/commcarepublic/List+of+xpath+functions

Amelia

On Tue, Oct 9, 2012 at 11:35 AM, Prabhas Pokharel <prabhas....@gmail.com wrote:

Hi all,
I’m wondering about how to do rounding in commcare. We are trying to
calculate the number of weeks in a pregnancy based on lmp_date and today’s
date. Its a simple division and round issue.

ODK’s javarosa source now supports “int()” (see http://opendatakit.org/
**help/form-design/binding/http://opendatakit.org/help/form-design/binding//
https://bitbucket.org/m.sundt/javarosa/src/62409ae3b803/
core/src/org/javarosa/xpath/**expr/XPathFuncExpr.java#cl-129https://bitbucket.org/m.sundt/javarosa/src/62409ae3b803/core/src/org/javarosa/xpath/expr/XPathFuncExpr.java#cl-129if you need details).

What is the commcare equivalent / is there one? (We tried round, but it
didn’t work).
cheers,
–prabhas