# Using MODULUS on a number, weird results

**URL:** <https://forum.inductiveautomation.com/t/using-modulus-on-a-number-weird-results/10409>\
**Category:** Ignition\
**Created:** [August 27, 2015, 6:17pm UTC](https://forum.inductiveautomation.com/t/using-modulus-on-a-number-weird-results/10409 "2015-08-27T18:17:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjgonzales](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pjgonzales](https://forum.inductiveautomation.com/u/pjgonzales)\
**Post date:** [August 27, 2015, 6:17pm UTC](https://forum.inductiveautomation.com/t/using-modulus-on-a-number-weird-results/10409/1 "2015-08-27T18:17:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pjgonzales](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pjgonzales](https://forum.inductiveautomation.com/u/pjgonzales)\
**Post date:** [August 27, 2015, 6:36pm UTC](https://forum.inductiveautomation.com/t/using-modulus-on-a-number-weird-results/10409/2 "2015-08-27T18:36:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 27, 2015, 6:40pm UTC](https://forum.inductiveautomation.com/t/using-modulus-on-a-number-weird-results/10409/3 "2015-08-27T18:40:07Z")

</div>

Can you use floor() before toInt()?

---

<div class="post-metadata">

**Author:** ![pjgonzales](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pjgonzales](https://forum.inductiveautomation.com/u/pjgonzales)\
**Post date:** [August 27, 2015, 7:45pm UTC](https://forum.inductiveautomation.com/t/using-modulus-on-a-number-weird-results/10409/4 "2015-08-27T19:45:48Z")

</div>

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

```auto
floor((4.97 * 12) % 12)
```

Result is 11. Is the result not an Int?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 27, 2015, 7:56pm UTC](https://forum.inductiveautomation.com/t/using-modulus-on-a-number-weird-results/10409/5 "2015-08-27T19:56:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [September 3, 2015, 12:51pm UTC](https://forum.inductiveautomation.com/t/using-modulus-on-a-number-weird-results/10409/6 "2015-09-03T12:51:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fourmajor](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@fourmajor](https://forum.inductiveautomation.com/u/fourmajor)\
**Post date:** [August 24, 2017, 2:12pm UTC](https://forum.inductiveautomation.com/t/using-modulus-on-a-number-weird-results/10409/7 "2017-08-24T14:12:38Z")

</div>

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

```auto
arg1 % arg2

```

always return less than arg2? Currently I have:

```auto
(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:

```auto
(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:

```auto
(3) % 3

```

it returns 0 as it should.

Any ideas?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 24, 2017, 2:35pm UTC](https://forum.inductiveautomation.com/t/using-modulus-on-a-number-weird-results/10409/8 "2017-08-24T14:35:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fourmajor](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@fourmajor](https://forum.inductiveautomation.com/u/fourmajor)\
**Post date:** [August 24, 2017, 2:44pm UTC](https://forum.inductiveautomation.com/t/using-modulus-on-a-number-weird-results/10409/9 "2017-08-24T14:44:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 24, 2017, 2:51pm UTC](https://forum.inductiveautomation.com/t/using-modulus-on-a-number-weird-results/10409/10 "2017-08-24T14:51:07Z")

</div>

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