Truncating a float


(Shawn) #1

I have a value (3.35) that I need to truncate to 3.3. This is because I believe it is affecting my average totals. I’ve attempted int((value *20) div 20, and round(value * 10) dive ten, one rounds down to 3 and the other rounds me up to 3.4


How can this be achieved?

This is initially based off an initial question that shows the value (3.35) highlighted in blue:


(Clayton Sims) #2

Hi Rich,

I think

int(X * 10) / 10

works in CommCare. Is there a reason you are trying

int(X * 20) / 20

? I’m not sure why it wouldn’t truncate the same way, but I’ve also never tried it and am not sure if there are any odd effects to not using a power of 10.

-Clayton


(Shawn) #3

Hi Clayton,

I was using 20^ because the compareh.q. page stated " to round to a different number of decimal places" use 2, 20, 2000 etc. here: https://confluence.dimagi.com/display/commcarepublic/Rounding+numbers

I am really interested in truncating because, I do not need it to round down or up, just take off the additional decimal places that I do not need. i.e. ’ 4.06’ has been rounding to ‘4.1’ and I need it to round to 4.0

^


(Clayton Sims) #4

Hi Shawn,

I might still be confused. The page you sent reads as follows to me

To round to two decimals change the 10’s in the formula above to 100. To three decimals change them to 1000, etc.

Did you attempt to perform the calculation with 10, instead of 20?

-Clayton


(Shawn) #5

Hi Clayton, I did attempt the calculation with 10 instead of 20, and still have the same rounding issue.


(Clayton Sims) #6

Hi Shawn,

the truncation logic appears to be working for me using int(input * 10) div 10

So I’m not sure what to suggest.

It may be helpful to work backwards and see if manually inputting the same value into your truncation hidden value’s input produces the same output you are seeing, to see if the problem is with the associated input


(Shawn) #7

Hi Clayton,

Thank you, for the update, I added int(input* 10 ) div 10 and it worked. Thanks for the followup and diligence.