# Casting String to Integer with Expression

**URL:** <https://forum.inductiveautomation.com/t/casting-string-to-integer-with-expression/26233>\
**Category:** Ignition\
**Created:** [July 2, 2019, 5:18pm UTC](https://forum.inductiveautomation.com/t/casting-string-to-integer-with-expression/26233 "2019-07-02T17:18:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![David.Reed.Schmidt](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@David.Reed.Schmidt](https://forum.inductiveautomation.com/u/David.Reed.Schmidt)\
**Post date:** [July 2, 2019, 5:18pm UTC](https://forum.inductiveautomation.com/t/casting-string-to-integer-with-expression/26233/1 "2019-07-02T17:18:21Z")

</div>

If I have an OPC tag that returns a string and I would like to convert it to an integer then what is the expression statement to do this? For example, if the strings look like any one of these:

0.0?  
125.0?  
99.5?

I would like to convert them to the following float values

0.0  
125.0  
99.5

The ‘?’ is part of what the device returns. I used the replace function to remove the ‘?.’ I used the replace function to remove the ‘.’ and then cast it to integer and float but I get an evaluation error. The evaluation error goes away when I cast to string but all I get are '‘00’, ‘1250’, and ‘995’

---

<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:** [July 2, 2019, 5:19pm UTC](https://forum.inductiveautomation.com/t/casting-string-to-integer-with-expression/26233/2 "2019-07-02T17:19:27Z")

</div>

`toFloat` doesn’t work?

---

<div class="post-metadata">

**Author:** ![David.Reed.Schmidt](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@David.Reed.Schmidt](https://forum.inductiveautomation.com/u/David.Reed.Schmidt)\
**Post date:** [July 2, 2019, 5:20pm UTC](https://forum.inductiveautomation.com/t/casting-string-to-integer-with-expression/26233/3 "2019-07-02T17:20:02Z")

</div>

Same issue. I even have the tag data type as a Float or an Integer.

Here is the expression

toFloat(replace(replace({[~]Tag},’?’,’’), ‘.’,’’))

Where {[~]Tag} is a string that equals ‘0.0?’

---

<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:** [July 2, 2019, 5:24pm UTC](https://forum.inductiveautomation.com/t/casting-string-to-integer-with-expression/26233/4 "2019-07-02T17:24:59Z")

</div>

Both of these work for me:  
`replace({[~]StringTag.value}, '?', '')`  
`toFloat(replace({[~]StringTag.value}, '?', ''))`

The first one gets implicitly cast to a float if the expression tag is configured as a Float.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 2, 2019, 5:25pm UTC](https://forum.inductiveautomation.com/t/casting-string-to-integer-with-expression/26233/5 "2019-07-02T17:25:07Z")

</div>

You should only need one `replace` operation, if you want to keep the decimal precision in the output:  
`toFloat(replace({[~]exp/WriteableString1}, "?", ""))`  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/b/b7dc53b219c38162d0cca8430565d169cf56857e.png)

---

<div class="post-metadata">

**Author:** ![David.Reed.Schmidt](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@David.Reed.Schmidt](https://forum.inductiveautomation.com/u/David.Reed.Schmidt)\
**Post date:** [July 2, 2019, 5:31pm UTC](https://forum.inductiveautomation.com/t/casting-string-to-integer-with-expression/26233/6 "2019-07-02T17:31:53Z")

</div>

Here is the issue. The string came from an OPC tag and defaulted to 5 characters. In order to convert the value to a float value I needed to trim the excess characters. Here is the final expression:

toFloat(replace(trim({[~]Tag}),"?",""))

Thanks for helping!

---

<div class="post-metadata">

**Author:** ![Paulo\_Custodio](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paulo_custodio/32/56599_2.png) [@Paulo\_Custodio](https://forum.inductiveautomation.com/u/Paulo_Custodio)\
**Post date:** [February 4, 2025, 9:09pm UTC](https://forum.inductiveautomation.com/t/casting-string-to-integer-with-expression/26233/7 "2025-02-04T21:09:03Z")

</div>

Had the exact same issue today, and this fixed it. Thank you!
