Format of unit items in strings

Hi community!

I use unit items like this:

Number:Energy BezugHeute "Bezug Strom heute [%.1f kWh]" (Stromzaehler, Haus)

and I want to paste them into a string to send a status email. The message string for my email is collected in that way:

message += "- Heute, Bezug: "+BezugHeute.state +"\t Einspeisung: "+EinspeisungHeute.state+"\n"

The problem is, that in the final string, the unit item is printed with the full length of decimal signs (in this example four, but sometimes much more…):

  • Heute, Bezug: 6.4445 kWh Einspeisung: 4.5357 kWh

Could anybody help me in formatting the unit item easily when pasting it into the string or using the defined format with one decimal sign? By the way, in the web UI the item is displayed like expected with one decimal sign.

Thanks guys!

One way is to use String.format.

message += String.format("- Heute, Bezug: %.1f\t kWh Einspeisung: %.1f kWh",
                         (BezugHeute.state as QuantityType<?>).floatValue,
                         (EinspeisungHeute.state as QuantityType<?>).floatValue)

Perfect, works like expected.
Thanks for your support!