Decimal, Number and Integer Confusion

Hi All,

I’ve always struggled with number formats in openhab rules but got something up and running about a year ago but I’ve just noticed that the numbers aren’t comparing well.

var setPos = currentSet1.state.toString
val actualTemp = (xrf_temp1.state as DecimalType).intValue
val Number setTemp = new Integer(setPos) 

These are my inputs. Basically a temperature set point that comes in as a string as it can sometimes be ON or OFF as well and then a 1Wire temperature which is a Number in the item file.

The set point will be 17 but the 1Wire temperature will be 17.00

Once I’ve done this conversion I can then compare them together.

if (actualTemp > setTemp) .....

However I noticed that 16.9 > 17 is false (correct) but 17.1 > 17 is also false (incorrect). 18.1 > 17 is true (correct).

I can only assume that the actualTemp is being stripped of it’s decimal point and is actually comparing 17 > 17.

How can I maintain the decimal points and put one on setTemp?

Many thanks

You are correct, the integer value of 17.1 is 17 and therefore (17 > 17) == false.

val actualTemp = (xrf_temp1.state as DecimalType);
val DecimalType setTemp = new DecimalType(setPos);

Don’t know if the above works, haven’t tried it. But you need to use something that preserves the floating point value, like Double or DecimalType.