# Replace Leading/Trailing 0's with Whitespace (stringFormat)

**URL:** <https://forum.inductiveautomation.com/t/replace-leading-trailing-0s-with-whitespace-stringformat/57938>\
**Category:** Ignition\
**Tags:** ignition80, vision\
**Created:** [April 1, 2022, 4:13pm UTC](https://forum.inductiveautomation.com/t/replace-leading-trailing-0s-with-whitespace-stringformat/57938 "2022-04-01T16:13:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [April 1, 2022, 4:13pm UTC](https://forum.inductiveautomation.com/t/replace-leading-trailing-0s-with-whitespace-stringformat/57938/1 "2022-04-01T16:13:57Z")

</div>

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.

---

<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:** [April 1, 2022, 4:17pm UTC](https://forum.inductiveautomation.com/t/replace-leading-trailing-0s-with-whitespace-stringformat/57938/2 "2022-04-01T16:17:46Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [April 1, 2022, 4:27pm UTC](https://forum.inductiveautomation.com/t/replace-leading-trailing-0s-with-whitespace-stringformat/57938/3 "2022-04-01T16:27:59Z")

</div>

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()](https://docs.inductiveautomation.com/display/DOC81/stringFormat) function or in the [Data Type Formatting Reference](https://docs.inductiveautomation.com/display/DOC81/Data+Type+Formatting+Reference), or the different characters used for each data type.

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [April 1, 2022, 4:31pm UTC](https://forum.inductiveautomation.com/t/replace-leading-trailing-0s-with-whitespace-stringformat/57938/4 "2022-04-01T16:31:01Z")

</div>

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.

> **[Anatomy of format mini-language](https://www.wikipython.com/other-concepts/anatomy-of-format-mini-language/)**
>
> When the format function replaced buggy old style “interpolation” formatting (the one with the % substitution character) the mini-language of the format specification string was expande…

---

<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:** [April 1, 2022, 4:34pm UTC](https://forum.inductiveautomation.com/t/replace-leading-trailing-0s-with-whitespace-stringformat/57938/5 "2022-04-01T16:34:07Z")

</div>

> [@zacht](#):
>
> it’s a python thing.

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

[https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html](https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html)

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [April 14, 2022, 6:08pm UTC](https://forum.inductiveautomation.com/t/replace-leading-trailing-0s-with-whitespace-stringformat/57938/6 "2022-04-14T18:08:40Z")

</div>

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.

---

<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:** [April 14, 2022, 7:52pm UTC](https://forum.inductiveautomation.com/t/replace-leading-trailing-0s-with-whitespace-stringformat/57938/7 "2022-04-14T19:52:14Z")

</div>

> [@amarks](#):
>
> Usign `%3.2f` I would expect to get `_ _ _._ _`

No, the "3" is the desired minimum length for the _ **whole string** _. You want `%6.2f` for that particular output.
