openHAB widget - Component 'label' round decimal number

I ended up solving this with (note: edited to include @JustinG suggestion in his reply right below):

  • In item definitions, change the item state presentations to always be floats, with 0 decimals when I want to see an integer without decimals. For instance: "Inverter Total Output Power Sunsynk [%.0f W]".
    Important note: setting it to %d feels more natural, but for some reason it doesn’t work!
  • in my Main UI widget, use the @"item" shortcut to use displayState if defined, otherwise fall back to state.
    For instance:
    text: =@"solar_total_power"
    This is because displayState is only defined if it defers from state. For instance in my case displayState only gets defined if the underlying item value isn’t a round integer.

With this I do get the expected format, and the UI code then makes no assumption on what unit is exported by the underlying item, so no risk of mismatch.