How can I round a value to 2 digits

val String DnAvgString = String.format("%.2f", DnAvg)
logInfo("dryer ", "Power average is: " + DnAvgString)

different error???

2019-01-14 18:48:33.628 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Dryer Consumption State Machine': f != org.eclipse.smarthome.core.library.items.NumberItem

Old post I know, but for completeness this should work instead…

var DnAvgString = String.format("%.2f", DnAvg) 
logInfo("dryer ", "Power average is: " + DnAvgString)

Had the same issue with

tempSetWarm = Math.round((roomThermostatSetTemp007402363C0A.state as Number)/100)*100

and then added a .floatValue as this:

tempSetWarm = Math.round((roomThermostatSetTemp007402363C0A.state as Number).floatValue/100)*100

and then it worked!

Hi there,

can you give me an advice how to half round a number(int) to x.5 or x.0 depent on input with a rule ​?

Example:

  • Input 21.2 → round to 21.5
  • Input 22.6 → round to 23.0
  • Input 19.8 → round to 20.0
  • Input 20.1 → round to 20.5

BR

For a start the input numbers are no int (as in integer), further you won’t to round UP always to the next half value.
In order to give code hints we would need to know what kind of rule you are trying to use.

PseudoCode:
Seperate the integer part of the number and the decimal part.
If the decimal part is less or equal to .5 return the integer part plus .5,
else return the integer part plus 1.

1 Like

Alternative process;
subject number multiply x 2
round to integer
divide by 2

2 Likes

Thanks! That´s quite simple. I did not think of that.

In practice i controll Homatic radioator thermostats depent on a target temperature and the actual temperature in the room. The HM devices only accept. x.0 or x.5.

BR