# Format of unit items in strings

**URL:** <https://community.openhab.org/t/format-of-unit-items-in-strings/143805>\
**Category:** Scripts & Rules\
**Tags:** openhab3\
**Created:** [January 26, 2023, 5:21pm UTC](https://community.openhab.org/t/format-of-unit-items-in-strings/143805 "2023-01-26T17:21:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Mayr](https://community.openhab.org/user_avatar/community.openhab.org/alexander_mayr/32/37224_2.png) [@Alexander\_Mayr](https://community.openhab.org/u/Alexander_Mayr)\
**Post date:** [January 26, 2023, 5:21pm UTC](https://community.openhab.org/t/format-of-unit-items-in-strings/143805/1 "2023-01-26T17:21:04Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [January 26, 2023, 5:59pm UTC](https://community.openhab.org/t/format-of-unit-items-in-strings/143805/2 "2023-01-26T17:59:56Z")

</div>

One way is to use String.format.

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

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Mayr](https://community.openhab.org/user_avatar/community.openhab.org/alexander_mayr/32/37224_2.png) [@Alexander\_Mayr](https://community.openhab.org/u/Alexander_Mayr)\
**Post date:** [January 27, 2023, 4:59am UTC](https://community.openhab.org/t/format-of-unit-items-in-strings/143805/3 "2023-01-27T04:59:42Z")

</div>

Perfect, works like expected.  
Thanks for your support!
