Script to format tag as string using engUnit & formatstring properties

I feel like this should be an integral feature of Ignition scripting and I’m just missing it, but I’ve not been able to find any hints in the manual or the forums. If I’m working in Perspective in a script, what is the recommended method to start from a tag path and end up with a string containing the current value, but properly formatted using properties of the tag like EngUnit and FormatString? Meaning if it was a floating value of 2.1023 with engUnit=“V” and formatstring="###0.0" I would want the return string to be “2.1 V”.

I can get all the properties and the value at once using system.tag.getConfig(tag_path,True), but then I still have to do a bit of work putting it all back together, and I’m not sure how to handle formatstring specifically.

I’m not aware of an easy way to parse these format strings into something useful in Python. Could what you’re trying to do be accomplished with a binding?

Going down the route of binding to each value gets very complicated quickly in my case. At a very high level I’m trying to fill in the data for a table component on the fly based on a mix of generated data and tag values. If the total number of rows was fixed I could plausibly bind at the cell level instead of the whole data table, but I’d have to create a buttload of bindings…

I’m more intrigued by the this post that implies the FormatString is handled by the java.text DecimalFormat class, and the fact that I can do this in the scripting console:

>>> from java.text import DecimalFormat
>>> formatter = DecimalFormat("#,##0.0")
>>> formatter.format(2.1023)
u'2.1'

The actual work of piecing it all together just got much easier…

1 Like

Looks like that should do the trick.:+1: I haven’t played with the table component in Perspective yet, but wonder if you could set format per column–assuming all the data in one column gets the same format (may not be a good assumption, based on what you’ve posted).

This helped me. I was reading opc tag values out of a tag, converting the value to another unit system and wanted to format the new value in the same way before putting it into a data table.
The java.text Decimal Format example worked well for this