# Help with \*checks notes\* simple division

**URL:** <https://forum.inductiveautomation.com/t/help-with-checks-notes-simple-division/51896>\
**Category:** Ignition\
**Created:** [October 13, 2021, 2:37pm UTC](https://forum.inductiveautomation.com/t/help-with-checks-notes-simple-division/51896 "2021-10-13T14:37:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [October 13, 2021, 2:37pm UTC](https://forum.inductiveautomation.com/t/help-with-checks-notes-simple-division/51896/1 "2021-10-13T14:37:47Z")

</div>

![Capture2](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/b/3b5eb632965b57c0d6b2d0dc681e2ca9b59f006f.png)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [October 13, 2021, 2:50pm UTC](https://forum.inductiveautomation.com/t/help-with-checks-notes-simple-division/51896/2 "2021-10-13T14:50:59Z")

</div>

The script is preforming Integer Division, which in this case is 0 (well 0 r1).

The simplest method would be to make one or both of the numbers floating point numbers

```auto
value = 1.0/2
value = 1/2.0
value = 1.0/2.0

```

You could also cast one to a float, but it must be prior to the division operation.

```auto
value = float(1)/2 #yields 0.5
value = 1/float(2) #yields 0.5
value = float(1)/float(2) #yields 0.5
value = float(1/2) #yields 0 

```

but that is ugggggly.

The advanced way is to import the future division operator:

```auto
from __future__ import division
value = 1/2 #yields 0.5

```

Note: with the future import you can still get the floored answer by using the `//` operator

```auto
from __future__ import division
value = 1//2 #yields 0

```

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [October 13, 2021, 2:53pm UTC](https://forum.inductiveautomation.com/t/help-with-checks-notes-simple-division/51896/3 "2021-10-13T14:53:17Z")

</div>

> [@lrose](#):
>
> `from __future__ import division`

I get an error when I use

`from __future__ import division`

* * *

I have been able to use

`import datetime`

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [October 13, 2021, 3:06pm UTC](https://forum.inductiveautomation.com/t/help-with-checks-notes-simple-division/51896/4 "2021-10-13T15:06:59Z")

</div>

ahh, didn’t think about that.

The from future import has to be at the top of the file, so it wont work from inside of a script transform, unless there’s some sneaky way to insert it that I’m unaware of.

Assuming you’re not just doing this simple operation, you could potentially put this in a project script.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/b/7b74d1a793ffc410be07ee3a2dca96ec7e52842f.png)

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/5/a59de5e28c5ac80862efe7d9eccfc05d1bff4393.png)

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [October 13, 2021, 3:14pm UTC](https://forum.inductiveautomation.com/t/help-with-checks-notes-simple-division/51896/5 "2021-10-13T15:14:33Z")

</div>

I am not sure of the implications of that.

I better declare the variables as floats when dividing in a script.

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [October 13, 2021, 3:28pm UTC](https://forum.inductiveautomation.com/t/help-with-checks-notes-simple-division/51896/6 "2021-10-13T15:28:31Z")

</div>

@lrose how do I set the value to show two decimals as percentage?

0.00%  
50.00%

I used the percent format, but the format pattern is not available

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [October 13, 2021, 3:37pm UTC](https://forum.inductiveautomation.com/t/help-with-checks-notes-simple-division/51896/7 "2021-10-13T15:37:02Z")

</div>

Well, you could add an additional format transform set to pattern with a pattern of #.00%, you would have to do the multiplication in the script prior to the return. Or you can do it all in the script (my preferred method, don’t really like stacking transforms if I can avoid it.

```auto
return '{0:.2f}%'.format((1/2.0)*100)

```

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [October 13, 2021, 3:39pm UTC](https://forum.inductiveautomation.com/t/help-with-checks-notes-simple-division/51896/8 "2021-10-13T15:39:42Z")

</div>

> [@lrose](#):
>
> `return '{0:.2f}%'.format((1/2.0)*100)`

```auto
 temp= value
 	if temp==0:
		value=0
 	else :
 		value=float(temp)*0.00003472222 			

return return '{0:.2f}%'.format(value)

```

works, thanks

Thanks very much @lrose

---

<div class="post-metadata">

**Author:** ![chandler](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@chandler](https://forum.inductiveautomation.com/u/chandler)\
**Post date:** [October 13, 2021, 4:26pm UTC](https://forum.inductiveautomation.com/t/help-with-checks-notes-simple-division/51896/9 "2021-10-13T16:26:11Z")

</div>

EDIT: just saw this is a repeat of lrose’s answer. oops.

division between a float and an int returns a float, so you can convert one of the numbers to a float and it will give you the “real” answer:

```auto
>>> float(1)/2
0.5

```

if you are dividing literals you can type one of them as a float literal rather than an int literal by adding a decimal:

```auto
>>> 1.0/2
0.5

```

If you are dividing using variables that are ints then the second option won’t work, but the first option will:

```auto
>>> a = 1
>>> b = 2
>>> float(a)/b
0.5

```
