Only one decimal in DecimalType

i get an item value with this:
`var Number current_temp = INGRESSO_CURRENT_TEMP.state as DecimalType

sometime it have more tha 1 decimal. I need to cut it at 1 decimal, without round. I mean that if value is between 20.50 and 20.59 it should always be 20.5 as DecimalType.

Many thanks for help.


The easiest would be to multiply by 10 into a long then divide by 10 back into a double.

val long tmp = current_temp * 10
val double truncated = tmp/10.0

To handle some weird edge cases and casting you can use the Math::floor method.

val truncated = Math::floor(current_temp * 10) / 10.0

Hi, I tried both.

1st example give me an error because current_temp is a DecimalType and not a long
2nd example expect current_temp to be a Double, but it is a DecimalType and does not work.

Can I cast value and make one on 2 method works?


You need to call current_temp.doubleValue to get the actual number out of it.

Hi @rlkoshak
using this:

val Double truncated = Math::floor(current_temp.doubleValue * 10) / 10.0

I always get error by IDE. Incompatible types, expected double but was bigDecimal

Try getting rid of the “Double” for truncated:

val truncated = Math::floor(current_temp.doubleValue * 10) / 10.0

If that doesn’t work try:

val double truncated = (Math::floor(current_temp.doubleValue * 10) / 10.0).doubleValue

hi @rlkoshak
same result with both example.


I’m out of ideas.

Designer complains but does the rule fail when it runs?

I have not ried to run, I have not access to server, I have designer locally.
tomorrow I’ll try and I’ll back with results.

Thanks and Regards

Hi @rlkoshak
the rule is not executed.


Hi @rlkoshak
i tried this code:

var full_temp = current_temp var DecimalFormat df = new DecimalFormat("#.#") var s = df.format(full_temp)

This format in my variable s a string with number I need, but I’m not able to convert back to DecimalType needed in rest of the rule


For future reference here is the code:

var DecimalFormat df = new DecimalFormat("#.#") var s = df.format(current_temp) current_temp = new DecimalType(s)

Glad you got it working. Unfortunately dealing with numbers and math in rules can be surprisingly challenging.

I touched with my hand :smile:
Thanks to put me on right way