Using MODULUS on a number, weird results

I am using the following expression in a data display:

toInt((tag({Root Container.Label.pv}) * 12) % 12)

The value coming into the tag is 4.97. Using this expression I get a result of 12, which doesn’t make sense. If I punch the same numbers into Excel [=MOD(INT(4.97 * 12 ))] I get a result of 11. Also if I punch the numbers into a calculator (4.97 * 12 % 12) I get 11.64 (convert to INT and the answer is 11). Am I doing something wrong? Am I missing something? Thanks for any help.

PJ Gonzales

I see what is happening here, but not sure what I can do about it.

  1. I put the following expression into a label: (4.97 * 12) % 12, and get the result 11.64
  2. If I add toInt to the expression: toInt((4.97 * 12) % 12), the result rounds up to the nearest integer. Instead of getting 11 from 11.64, I get 12.

This is really messing up my conversion. Does anyone know what can be done to fix this issue?

Can you use floor() before toInt()?

Would it be necessary to use toInt after floor()? That seems to return the result that I want.

floor((4.97 * 12) % 12)

Result is 11. Is the result not an Int?

I think it’s technically a float, since a float shows up in the expression, but if it does what you want… :wink:

The floor function throws away the fractional part of the expression and returns the biggest possible integer, so you don’t need to use toInt.

I am having a similar problem. Is it just me or should:

arg1 % arg2

always return less than arg2? Currently I have:

(getHour24(dateArithmetic(now(), -8, "hour")) / 8) % 3

and depending on the hour it sometimes returns 3. This seems like a bug to me. The first part of the expression:

(getHour24(dateArithmetic(now(), -8, "hour")) / 8)

returns 3 before 8 AM. And yet if I substitute 3 for this first part of the expression, running:

(3) % 3

it returns 0 as it should.

Any ideas?

The result of the mod operation is not wrong, you just have some rounding happening where you don’t expect it.

This expression: (getHour24(dateArithmetic(now(), -8, "hour")) / 8) leaves you with a decimal result (2.88 for example). 2.88 % 3 is still 2.88. Put 2.88 into an integer tag and it gets rounded up to 3.

Thanks for clearing that up! I wasn’t aware that getHour24 returned a decimal.

It doesn’t, but that division by 8 leaves you with a decimal.