marcolino7
(marcolino)
November 23, 2015, 12:32pm
1
Hi,
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.
Marco`
rlkoshak
(Rich Koshak)
November 23, 2015, 4:27pm
2
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
marcolino7
(marcolino)
November 23, 2015, 4:33pm
3
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?
Regards
rlkoshak
(Rich Koshak)
November 23, 2015, 5:52pm
4
You need to call current_temp.doubleValue
to get the actual number out of it.
marcolino7
(marcolino)
November 23, 2015, 9:51pm
5
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
rlkoshak
(Rich Koshak)
November 23, 2015, 10:39pm
6
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
marcolino7
(marcolino)
November 23, 2015, 10:58pm
7
hi @rlkoshak
same result with both example.
Marco
rlkoshak
(Rich Koshak)
November 23, 2015, 11:09pm
8
I’m out of ideas.
Designer complains but does the rule fail when it runs?
marcolino7
(marcolino)
November 23, 2015, 11:12pm
9
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
marcolino7
(marcolino)
November 24, 2015, 9:15am
10
Hi @rlkoshak
the rule is not executed.
Marco
marcolino7
(marcolino)
November 24, 2015, 9:50am
11
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
Thanks
marcolino7
(marcolino)
November 24, 2015, 9:55am
12
For future reference here is the code:
var DecimalFormat df = new DecimalFormat("#.#") var s = df.format(current_temp) current_temp = new DecimalType(s)
rlkoshak
(Rich Koshak)
November 24, 2015, 3:49pm
13
Glad you got it working. Unfortunately dealing with numbers and math in rules can be surprisingly challenging.
marcolino7
(marcolino)
November 24, 2015, 4:04pm
14
I touched with my hand
Thanks to put me on right way
Regards