How to "Format" a number received from MQTT

Dear All,
I’m a Beginner,
From the Photovoltaic Inverter ( SolarEdge ) I get a actual power generated, via MQTT, in this format :
image
Of course I don’t need 4 decimal and I need to have only the integer.
What could be the simplest way to cut the decimals ?
Of course, the actual power is in W , not kW as shown in the footer …
Many Thanks in advance for any help.
Kind Regards

  • openHAB version:3.0.2

you can use the code snippet below to round to two digits after the comma.

Math.round(yourNumnber * 100) / 100

You can try it out at Mozillas very well written documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round:
put this line into the text box console.log(Math.round(0.91534 * 100) / 100); and then hit Run to see the result. It will result to 0.92

See for example also: https://stackoverflow.com/questions/15762768/javascript-math-round-to-two-decimal-places

To cast a received value to a number, you can do Number(receivedString) or just divide by 1 receivedString / 1. But I think, javascript will try to map any given value to a number before doing the Math.round() method. If the casting fails, you will get an respective error message.

Many Thanks for prompt reply, But really I don’t know were to put this code…
no way to use some parameter in Json transformation already in use for this MQTT data ?
Or other short road to format the result ?

Regards

You probably don’t need to mess with the Item’s state, this is just about UI presentation?
Use the state presentation metadata to format for display.