# Convert decimal to float

**URL:** <https://forum.inductiveautomation.com/t/convert-decimal-to-float/80901>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [October 24, 2023, 12:50pm UTC](https://forum.inductiveautomation.com/t/convert-decimal-to-float/80901 "2023-10-24T12:50:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benito\_Anton\_Recio](https://avatars.discourse-cdn.com/v4/letter/b/ecd19e/32.png) [@Benito\_Anton\_Recio](https://forum.inductiveautomation.com/u/Benito_Anton_Recio)\
**Post date:** [October 24, 2023, 12:50pm UTC](https://forum.inductiveautomation.com/t/convert-decimal-to-float/80901/1 "2023-10-24T12:50:00Z")

</div>

I would like to know how to convert a value in decimal, that comes from opc (this means for example decimal 3, in boolean 00000000 00000011) to float format cause it's really a real (or float) value.

Thanks!

---

<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 24, 2023, 12:54pm UTC](https://forum.inductiveautomation.com/t/convert-decimal-to-float/80901/2 "2023-10-24T12:54:45Z")

</div>

> [@Benito\_Anton\_Recio](#):
>
> that comes from opc

Show us the details of how this value comes in (most drivers will interpret as floating point for you if you tell them to).

---

<div class="post-metadata">

**Author:** ![Benito\_Anton\_Recio](https://avatars.discourse-cdn.com/v4/letter/b/ecd19e/32.png) [@Benito\_Anton\_Recio](https://forum.inductiveautomation.com/u/Benito_Anton_Recio)\
**Post date:** [October 24, 2023, 1:27pm UTC](https://forum.inductiveautomation.com/t/convert-decimal-to-float/80901/3 "2023-10-24T13:27:55Z")

</div>

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

In the origin (a CNC machine control) it's a REAL value (or float).  
Later, this values is got by a Siemens gateway through mqtt (as picture attached) to publish it as opc ua.

In Siemens gateway, it's defined this value as float (as shown in picture, yellow marks) but gateway iterprets this value as decimal, instead of a float. (I think this is a bug of Siemens. I'll let them know)

As a result, I'm trying to change this type of value (decimal to float) in Ingnition, which is susbcribed to this Siemens gateway where the value is. And that's the reason why I need to convert from decimal to float, or maybe decimal-boolean-float, I don't know.

Thanks in advance,  
Benito.

---

<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:** [October 24, 2023, 1:31pm UTC](https://forum.inductiveautomation.com/t/convert-decimal-to-float/80901/4 "2023-10-24T13:31:28Z")

</div>

It may need to be done via scripting, using [Float.intBitsToFloat](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Float.html#intBitsToFloat(int))

```auto
>>> from java.lang import Float
>>> Float.intBitsToFloat(1125515264)
150.0
>>>

```

---

<div class="post-metadata">

**Author:** ![Benito\_Anton\_Recio](https://avatars.discourse-cdn.com/v4/letter/b/ecd19e/32.png) [@Benito\_Anton\_Recio](https://forum.inductiveautomation.com/u/Benito_Anton_Recio)\
**Post date:** [October 25, 2023, 6:17am UTC](https://forum.inductiveautomation.com/t/convert-decimal-to-float/80901/5 "2023-10-25T06:17:14Z")

</div>

> [@Kevin.Herron](#):
>
> `1125515264`

Hi Kevin!

I'm trying it but it doesn't work. The truth is this is the first time I import resources into code ("Float"). Could you tell me what I`m doing wrong?

The tag "MemoryGH2055\_maq" has a script as shown for writing its value in "MemoryGH2055" but as float.

Thanks in advance.

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

---

<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 25, 2023, 11:58am UTC](https://forum.inductiveautomation.com/t/convert-decimal-to-float/80901/6 "2023-10-25T11:58:07Z")

</div>

Looks like a missing indent on line 2. Look in the right margin of the code editor for little red marks at syntax errors.

---

<div class="post-metadata">

**Author:** ![Benito\_Anton\_Recio](https://avatars.discourse-cdn.com/v4/letter/b/ecd19e/32.png) [@Benito\_Anton\_Recio](https://forum.inductiveautomation.com/u/Benito_Anton_Recio)\
**Post date:** [October 25, 2023, 12:09pm UTC](https://forum.inductiveautomation.com/t/convert-decimal-to-float/80901/7 "2023-10-25T12:09:20Z")

</div>

Hi both!

Pturmel, you're right and I had already fixed the mistake, but also I had to define origin value as INTEGER and final value as FLOAT.

And now yes, it works!

Thanks so much!!

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

---

<div class="post-metadata">

**Author:** ![Yohili\_Reyes](https://avatars.discourse-cdn.com/v4/letter/y/977dab/32.png) [@Yohili\_Reyes](https://forum.inductiveautomation.com/u/Yohili_Reyes)\
**Post date:** [May 11, 2024, 1:14am UTC](https://forum.inductiveautomation.com/t/convert-decimal-to-float/80901/8 "2024-05-11T01:14:01Z")

</div>

You need to create a new OPC tag for the value that you want to convert. In the new tag, select the OPC client and the OPC value that you want. That fix you problem and is more clear and robust way.

---

<div class="post-metadata">

**Author:** ![Federico\_fabiancic1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Federico\_fabiancic1](https://forum.inductiveautomation.com/u/Federico_fabiancic1)\
**Post date:** [March 20, 2025, 4:05am UTC](https://forum.inductiveautomation.com/t/convert-decimal-to-float/80901/9 "2025-03-20T04:05:37Z")

</div>

Hi Kevin,  
I have a similar scenario where I have one float tag that it is split in two different OPC tags.  
I was able to use your code in a tag event script to join them and it works ( on a UDT).  
I have +1000 tags like this.  
**How this Tag Event script will impact in the performance of the gateway?**

My code is:

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
		from java.lang import Float
		values= system.tag.readBlocking(["[.]HMI[5]","[.]HMI[4]"])
		value = values[0].value <<16 | values[1].value
		valuefp= Float.intBitsToFloat( value)
		system.tag.writeBlocking(["[.]../SIM_AI"],[valuefp])

```

---

<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:** [March 20, 2025, 8:29am UTC](https://forum.inductiveautomation.com/t/convert-decimal-to-float/80901/10 "2025-03-20T08:29:29Z")

</div>

You could probably handle that by making `SIM_AI` an expression tag, using the other 2 in its expression.

In the script library:

```python
from java.lang import Float

def convert_to_float(int_val):
	return Float.intBitsToFloat(int_val)

```

expression:

```python
runScript(
	'test.convert_to_float', 0,
	{[.]sources/source}[0] << 16 | {[.]sources/source}[1]
)

```

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

I think it's cleaner/easier to follow, as it makes `SIM_AI` responsible for computing its value, and I'm willing to bet it's also faster.

edit: Wait, it seems the tag doesn't update when the source values change. I'm thinking it's because they're in an array...  
edit2: nevermind, it did change, but the formatting on the final tag was hiding it.
