# What have I missed, when worked with division in Jython?

**URL:** <https://forum.inductiveautomation.com/t/what-have-i-missed-when-worked-with-division-in-jython/84168>\
**Category:** General Discussion\
**Tags:** python\
**Created:** [January 12, 2024, 7:09am UTC](https://forum.inductiveautomation.com/t/what-have-i-missed-when-worked-with-division-in-jython/84168 "2024-01-12T07:09:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![hans\_odgaard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hans_odgaard/32/83110_2.png) [@hans\_odgaard](https://forum.inductiveautomation.com/u/hans_odgaard)\
**Post date:** [January 12, 2024, 7:09am UTC](https://forum.inductiveautomation.com/t/what-have-i-missed-when-worked-with-division-in-jython/84168/1 "2024-01-12T07:09:01Z")

</div>

Took me a while to figure out why my oee calculations was all zeros.  
But after a little testing I figured out that I had to treat all my input values as floats by using float(a) in my calculation in my scripts, is this a Jython 2.7.3 feature ?

/Hans

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/9/e9a50e8f4a4c30152f71120c0bf9192b6f2d6683.png)

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [January 12, 2024, 8:21am UTC](https://forum.inductiveautomation.com/t/what-have-i-missed-when-worked-with-division-in-jython/84168/2 "2024-01-12T08:21:09Z")

</div>

What you are experiencing is normal behavior for int division. If all ints are being used in the calculation, the result will be an int datatype. However, if a float is involved in the division, the result will be float. Consequently, if you need a float (decimal) answer, and all your inputs are ints, you will need to cast at least one of them to a float prior to the calculation or within the calculation itself.

---

<div class="post-metadata">

**Author:** ![David\_Stone](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/david_stone/32/79749_2.png) [@David\_Stone](https://forum.inductiveautomation.com/u/David_Stone)\
**Post date:** [January 12, 2024, 8:32am UTC](https://forum.inductiveautomation.com/t/what-have-i-missed-when-worked-with-division-in-jython/84168/3 "2024-01-12T08:32:08Z")

</div>

To add to this, python doesn't have distinct recognition of floats, so your declaration of an float as a static number can simply be:

```auto
a=7.0
b=3

print "Result int = ", (a/b)

```

---

<div class="post-metadata">

**Author:** ![hans\_odgaard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hans_odgaard/32/83110_2.png) [@hans\_odgaard](https://forum.inductiveautomation.com/u/hans_odgaard)\
**Post date:** [January 12, 2024, 9:45am UTC](https://forum.inductiveautomation.com/t/what-have-i-missed-when-worked-with-division-in-jython/84168/4 "2024-01-12T09:45:47Z")

</div>

thanks for responses but I tried this

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/f/0f60adc611cc1bc82deb1166495efbf5195f813d.png)

And this work fine

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [January 12, 2024, 10:12am UTC](https://forum.inductiveautomation.com/t/what-have-i-missed-when-worked-with-division-in-jython/84168/5 "2024-01-12T10:12:25Z")

</div>

> [@hans\_odgaard](#):
>
> And this work fine

Is that Python 2.7 (which is what Ignition's Jython is based on)?

---

<div class="post-metadata">

**Author:** ![hans\_odgaard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hans_odgaard/32/83110_2.png) [@hans\_odgaard](https://forum.inductiveautomation.com/u/hans_odgaard)\
**Post date:** [January 12, 2024, 10:24am UTC](https://forum.inductiveautomation.com/t/what-have-i-missed-when-worked-with-division-in-jython/84168/6 "2024-01-12T10:24:13Z")

</div>

No it was on my own Python 3.9.1 I just tried there for a test

---

<div class="post-metadata">

**Author:** ![David\_Stone](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/david_stone/32/79749_2.png) [@David\_Stone](https://forum.inductiveautomation.com/u/David_Stone)\
**Post date:** [January 12, 2024, 10:24am UTC](https://forum.inductiveautomation.com/t/what-have-i-missed-when-worked-with-division-in-jython/84168/7 "2024-01-12T10:24:48Z")

</div>

From the Jython Spec:

"For (plain or long) integer division, the result is an integer. The result is always rounded towards minus infinity: 1/2 is 0, (-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result is a long integer if either operand is a long integer, regardless of the numeric value"

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [January 12, 2024, 10:40am UTC](https://forum.inductiveautomation.com/t/what-have-i-missed-when-worked-with-division-in-jython/84168/8 "2024-01-12T10:40:25Z")

</div>

The division operator changed in python 3.x, it's now a float division operator.  
You get the same integer division behavior with a new operator: '//'  
For example:

```python
>>> 3 / 2
1.5
>>> 3 // 2
1

```

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [January 12, 2024, 10:55am UTC](https://forum.inductiveautomation.com/t/what-have-i-missed-when-worked-with-division-in-jython/84168/9 "2024-01-12T10:55:51Z")

</div>

> [@hans\_odgaard](#):
>
> No it was on my own Python 3.9.1

You need to test it in Designer's Script Console.

 ![Integer division](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/0/909de8a4e775a4972faa3fa1d858a3c57931baeb.png)

---

<div class="post-metadata">

**Author:** ![hans\_odgaard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hans_odgaard/32/83110_2.png) [@hans\_odgaard](https://forum.inductiveautomation.com/u/hans_odgaard)\
**Post date:** [January 12, 2024, 11:03am UTC](https://forum.inductiveautomation.com/t/what-have-i-missed-when-worked-with-division-in-jython/84168/10 "2024-01-12T11:03:00Z")

</div>

I know see my first post, but thanks for all the answers, I was just a little bit puzzled by the result I got, but now I know how to do it right. Or do you just need to define the datatype ?

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [January 12, 2024, 11:37am UTC](https://forum.inductiveautomation.com/t/what-have-i-missed-when-worked-with-division-in-jython/84168/11 "2024-01-12T11:37:33Z")

</div>

> [@hans\_odgaard](#):
>
> Or do you just need to define the datatype ?

Putting the number in `float()` is as close as you can get to declaring the datatype, but as David pointed out, just adding the decimal yourself makes the number behave as a float.
