Round an Item state

I have an Item of type “Number:Length”, an example value is:

logInfo("Snow", fc24hSnowVolume.state.toString)
0.0075000000000001 mm

(mm snow in forecast, as example)

How can I round this state to 0 or 1 decimal points, and keep the unit intact? When I try to apply Math.round(), I see an error message like:

Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

Any combination of “as DecimalType” or “as Number”, together with “floatValue” also doesn’t work.

Here’s a similar example used to generate a rounded string - might help!

Maybe something like this could work as well.

logInfo("Snow", ((( fc24hSnowVolume.state as Number)*100).intValue)/100.0)

If this for UI purpose you can add formatting to the item definition itself.
Can you post the item definition, please?

I tried that, ended up with an error message:

Error during the execution of rule 'Weather': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

It’s for something I want to send as text, not for the UI.

The type is “Number:Length”, as initially mentioned. The Item definition is:

Number:Length		fc24hSnowVolume		"Snow volume in 24h [%.1f %unit%]"	<snow>		{ channel="openweathermap:weather-and-forecast:home:forecastHours24#snow" }

To follow up on this, I went down the brute force way, split the string up, and then handle the parts separately:

var snow_value = fc24hSnowVolume.state.toString.split(' ').get(0)
var snow_value_rounded = Math::round(Float::parseFloat(snow_value))
var snow_unit = fc24hSnowVolume.state.toString.split(' ').get(1)

“snow_value_rounded” can be rounded to more decimal points, but in my case it’s enough to know if there will be snow at all. Same for rain, no one cares about “0.0038 mm” rain tomorrow …