# Round currentValue if certain conditions are met

**URL:** <https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476>\
**Category:** Ignition\
**Tags:** perspective, scripting\
**Created:** [October 13, 2023, 9:00pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476 "2023-10-13T21:00:03Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [October 13, 2023, 9:00pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/1 "2023-10-13T21:00:03Z")

</div>

I have a custom property with the following change script on it.

```auto
if self.custom.Units == 'Each':
	# Round current value to nearest integer value.
	currentValue.value = round(currentValue.value)
else:
	# User is permitted to enter decimal units.
	currentValue.value = currentValue.value
```

I am only ever going to receive integers or decimals and what I would like to do is round the entered decimal value if not a whole integer under certain conditions.

However, my script doesn't appear to work. I cannot tell why it is not working though. Anyone know what I am missing?

The following code also fails to round any entered values so I don't think it is my condition check.

```auto
if self.custom.Units == 'Each':
	# Round current value to nearest integer value.
	currentValue.value = round(currentValue.value)
else:
	# User is permitted to enter decimal units.
	currentValue.value = round(currentValue.value)
```

---

<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:** [October 13, 2023, 9:16pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/2 "2023-10-13T21:16:04Z")

</div>

On what component type is your custom property? A text field? A numeric entry field?

I know it's only test code but  
`currentValue.value = currentValue.value`  
isn't going to do much!

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [October 13, 2023, 9:21pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/4 "2023-10-13T21:21:20Z")

</div>

It is a numeric entry field with a binding to the custom property and I have bi-directional settings enabled on the binding.

That line of code isn't meant to do anything since I don't need to round any values if the _Units_ is not _Each_.

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [October 13, 2023, 9:31pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/5 "2023-10-13T21:31:20Z")

</div>

Isn't `currentValue` read only? I don't think you can assign anything to it in a script. Or at least if you do I don't think it propagates out.

You can set the value by doing a `self.props.value = round(currentValue.value)`. You will need to check the source of the change though so the script doesn't keep triggering itself with a value change, so you should have something along the lines of:

```auto
if (origin == 'Browser' or origin == 'Binding') and self.custom.units == 'Each':
	self.props.value = round(currentValue.value)

```

Edit: Whoops, `valueInt` is vision not perspective and so is `event.source`.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [October 13, 2023, 9:33pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/6 "2023-10-13T21:33:46Z")

</div>

Damn, you are right.

I think it is time to changeover to the weekend now.

---

<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:** [October 13, 2023, 9:34pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/7 "2023-10-13T21:34:20Z")

</div>

```auto
	if self.custom.Units == 'Each':
		# Round current value to nearest integer value.
		self.props.value = round(self.props.value)

```

You were rounding "Each" instead of the number!

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [October 13, 2023, 9:36pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/8 "2023-10-13T21:36:48Z")

</div>

> [@Transistor](#):
>
> You were rounding "Each" instead of the number!

Oh yeah, my change script should be on the value property, not the custom property, otherwise use Transistor's version.

Mine has the bonus of rounding anything anyone enters.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [October 13, 2023, 9:41pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/9 "2023-10-13T21:41:32Z")

</div>

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

Hmmmm, not sure if this is the proper check however.

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [October 13, 2023, 9:43pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/10 "2023-10-13T21:43:16Z")

</div>

I adjusted my answer as I realized you probably wanted it to adjust the value coming from the device as well. The check should be `if (event.origin =='Browser' or event.origin == 'Binding') and self.custom.units == 'Each'`

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [October 13, 2023, 9:43pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/11 "2023-10-13T21:43:43Z")

</div>

This is in Perspective... and it is mad about the word event.

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [October 13, 2023, 9:44pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/12 "2023-10-13T21:44:28Z")

</div>

> [@bmeyers](#):
>
> This is in Perspective... and it is mad about the word event.

🙃 🙃 🙃

Yeah, just use `origin`. Brain is in vision today

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [October 13, 2023, 9:46pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/13 "2023-10-13T21:46:55Z")

</div>

The script was refusing to work on the custom property binding so I moved it over to the numeric entry field and it works however it locks up for a few seconds sometimes so I am not 100% this is the cleanest way to do this.

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/4/34353e44c458627342f1ca4c559e7ff073473182.png)

Removing lines 5, 6, and 7 helped somewhat.

---

<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:** [October 13, 2023, 9:49pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/14 "2023-10-13T21:49:20Z")

</div>

> [@bmeyers](#):
>
> I have a custom property with the following change script on it.
> 
> ```auto
> if self.custom.Units == 'Each':
> 
> ```

I think you gave a bum steer. I read this as "the change script is on the custom property", and not on the value. Oh well ...

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [October 13, 2023, 9:49pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/15 "2023-10-13T21:49:32Z")

</div>

> [@bmeyers](#):
>
> The script was refusing to work on the custom property binding

Yeah, I thought you were on the value property until after I posted. Mine was intended for the value property not the custom property.

As for the lock up, remove `else` and everything beyond it. The script is re-triggering itself with that line. The normal value will stay even without you specifying the else.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [October 13, 2023, 9:50pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/16 "2023-10-13T21:50:03Z")

</div>

Ok, everything makes sense now. Thanks for the help.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 13, 2023, 11:11pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/17 "2023-10-13T23:11:14Z")

</div>

The bottom line is that you are trying to "fix" the value that is coming from a binding **in place**. This is an infinite-recursion trap, even if you can patch it with the origin information.

Move your binding to a custom property. Use and expression binding to that custom prop, with desired rounding operation, from the original binding location.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [October 16, 2023, 3:31pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/18 "2023-10-16T15:31:49Z")

</div>

What I ended up doing is adding two custom expression bindings on the numeric entry field. The first binding is the normal value entered by the user. The second value is the rounded value entered by the user. I realize I don't necessarily need the normal custom value but I put it there for readability purposes.

On the format property of the numerical entry field I put a binding:

```auto
if({/root.custom.Units}='Each', '0,0', '0,0.##')
```

If the Units is Each then an Integer format is applied. Alternatively a numerical format is applied.

I will then use logic elsewhere to determine if the normal value or rounded value should be used but the end user will always see what the system is planning on using in any calculations.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [October 16, 2023, 3:38pm UTC](https://forum.inductiveautomation.com/t/round-currentvalue-if-certain-conditions-are-met/80476/19 "2023-10-16T15:38:41Z")

</div>

Also the "lockup" of the numerical entry field seems to be related to whether or not I have the spinner enabled or not. If the spinner is disabled I have to click on the numeric entry field 2 or 3 times to get back in edit mode (or click on another component and refocus on the numeric entry field). If I have the spinner enabled I can edit it with a single click every time. Since it isn't a big deal overall I just enabled it again (it is enabled by default).
