I’m reviving this old thread just so this Rounding issue’s solutions might be in one place.
I am doing energy monitoring and the math is creating numbers like 249.363648099999973338954077917151153087615966796875. I want to send myself an email each morning with the use for yesterday, but obviously it is sending this super long number and I would like to round it. I have tried almost all the options I could find online and they all generate an error, usually about type mismatch. I have tried your string option now as well, but get this error:
2016-08-15 08:37:00.009 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Send usage email
java.util.IllegalFormatConversionException: f != org.openhab.core.library.types.DecimalType
Here is my rule with my email address hidden of course. You’ll see I commented out some of the previous tries:
rule "Send usage email"
Time cron "0 * * * * ?"
val energyMail = String::format("Energy use is %.1f", Energy_UseDaily.state as DecimalType)
// var energyMail = Energy_UseDaily.float as DecimalType
// var tmp = (Math::round(energyMail*10.0)/10.0)
sendMail("myemail", "Daily energy use", "Yesterday we used " + energyMail)
My Energy_UseDaily item looks like this: