Casting in expression

I am trying to cast a tag (float)value to a string inside of an expression within my component. If there is a null value, then the value should display "N/A". I am wrapping my value in the toStr() function to convert the value first. Then I have tried using both != None and None in my expression and I am getting 0.00 for the values still. Can anyone provide suggestions on how to resolve this?

//None
if( toStr({PouringFurnaceStatus_HWS_DISA.ATAS_SIe.value}) = None,
"N/A",
{PouringFurnaceStatus_HWS_DISA.ATAS_SIe.value}
)

// Not None
if(toStr({PouringFurnaceStatus_HWS_DISA.ATAS_SIe.value}) != None,
{PouringFurnaceStatus_HWS_DISA.ATAS_SIe.value},
"N/A"
)

Please see Wiki - how to post code on this forum.

Try using the isNull() expression function, and use it before you convert it to a string.

Here's a link to more info on this function:

You can also format your float while casting it to a string using the numberFormat() function:

if(
	isNull({PouringFurnaceStatus_HWS_DISA.ATAS_SIe.value}),
	"N/A",
	numberFormat({PouringFurnaceStatus_HWS_DISA.ATAS_SIe.value}, "#,##0.0")
)
4 Likes

Actually, the expression language accepts null, None, and none as a null constant.

6 Likes

The expression would be:

coalesce(toStr({PouringFurnaceStatus_HWS_DISA.ATAS_Sle}),"N/A")

Or if you wanted to also format it as @Brandon_Peterson says you could do:

coalesce(numberFormat({PouringFurnaceStatus_HWS_DISA.ATAS_Sle}, "#,##0.0"),"N/A")
2 Likes

I tried that too but im still getting 0.00 as the value that is being returned...

Can you show us a (nicely cropped) screen shot of your tag as it appears in Tag Browser along with the binding editor screen showing the Binding Preview results at the bottom.?

2 Likes

And where you're using it on this component?
Vision properties are strongly typed, so you can't return a string value from a float property.

1 Like

I tried that too but im still getting 0.00 as the value that is being returned...

Ahhh....I just realized that whoever created the template used numeric labels, so this is more than likely why I cant cast the float values to a string.

Remember that 0 ≠ NULL. As 0 is a valid value for a float, and coalesce is specifically looking for NULL.

If you want 0 to be printed as "N/A" then that expression would be:

if(
    {PouringFurnaceStatus_HWS_DISA.ATAS_Sle} == 0,
    "N/A",
    NumberFormat({PouringFurnaceStatus_HWS_DISA.ATAS_Sle},"#,##0.0")
)

Ive tried everything I know and I am still not able to cast the value.

Well, if the component you are using the expression on is actually a NumericLabel, then even if you did cast it to a string, it would just try to cast it back to a float again.

Put the expressions on a standard label and then see what happens.

If you're still not able to get it, then provide the information requested by @Transistor and @PGriffith

4 Likes