Replace Leading/Trailing 0's with Whitespace

I’d really like to have the label, the value, and the units for a tag all in one label component. However, this leads to either the label or unit (depending on leading/trailing alignment) shifting as optional digits pop in and out.

Has anybody come up with a nice way to replace leading/trailing 0’s in a formatted number with whitespace (blanks)?

For instance, a flow meter that flows up to 1000 gal. Would show “__50 gal” or “_120 gal” or “1000 gal” instead of “50 gal”, “120 gal”, and “1000 gal”, respectively.

Use the stringFormat() expression function with a plain label, and specify field widths for each substitution. Like so:

stringFormat("%10.2f", {Root Container.path.to.property})
1 Like

Thanks for this. This is something that’s really not documented in the user manual. I don’t see any reference to the 10.2 portion of the %10.2f format string in the documentation for the stringFormat() function or in the Data Type Formatting Reference, or the different characters used for each data type.

Corrected below: It’s not Ignition specific, it’s a python thing.

You could do a similar thing with python if you wanted, but then you’d have to use a script, not an expression.

Nope. It’s a java thing in this case. Many more options:

https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html

1 Like

When I use this I get an IllegalFormatConversionException: f != java.lang.Integer error.

I’m using %f and my number that’s being formatted is a float.

By adding toFloat() around my tags, I get rid of the error, but odd that just using a float tag directly causes the error.

However, this does not seem to have the desired effect. Usign %3.2f I would expect to get _ _ _._ _ but that is not the case. The extra spaces are still being discareded.

No, the “3” is the desired minimum length for the whole string. You want %6.2f for that particular output.

1 Like