Groovy - drivers - and rounding

Drivers should not round if not asked. I don't think this is a dev problem. I have device drivers I've written, other devs' work I use, and HE drivers. When a generic sensor reports a temp and offers me a decimal place - 58.8 for example, it shows 58.8 on my tiles. But if the temp is 58.0 it shows 58 on the tile.
From my reading this seems platform specific - even references to Groovy library versions. HE should give me the option at the OS level and I shouldn't be forced to accept rounding up on .0 values. It throws off tile layouts, fonts etc and looks crappy. rant over.

This is what I am using in one of my drivers:

def temperatureEvent( temperature ) {
    def map = [:] 
    map.name = "temperature"
    map.unit = "\u00B0"+"${location.temperatureScale}"
    String tempConverted = convertTemperatureIfNeeded(temperature, "C", precision=1)
    map.value = tempConverted
    if (settings?.txtEnable) {log.info "${device.displayName} ${map.name} is ${map.value} ${map.unit}"}
    sendEvent(map)
}
infoMotion 3in1 _TZE200_7hfcudw5 temperature is 30.0 °C
1 Like

There are definitely coding options to prevent the zero from being dropped regardless of what the value is representing. Something as simple as:

newStaticCharge = newStaticCharge.toBigDecimal().setScale(2,BigDecimal.ROUND_HALF_UP)

enforces maintaining two decimal places.

3 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.