Weird values - 40+ digits after decimal

I’ve seen this issue come and go, using OH2. In this example, I’m using the SystemInfo binding.

loadAverage15min changed from 1.8300000000000000710542735760100185871124267578125 to 1.810000000000000053290705182007513940334320068359375

What am I doing to get values like this, and how can I force one or two digits of precision?

Yea, this is common.

To elaborate, computers are really really bad at doing math with floating point numbers. They are basically incapable of generating an exact calculation when doing even the simplest of operations so what you end up with is a number that is really really close but not quite. This results in lots and lots of decimal places. It also results in the following conditional almost certainly returning false:

0.1 + 0.2 == 0.3 // computer will return false

To get two digit precision depends on in what context you want the two digits. Is it just for display/logs or do you truly only want it to keep up to the hundredths place and always round everything?

In the former you can use String.format for your log statements and % notation on your sitemap.

    val rounded = String::format("%.2f", floatValue)
Number LoadAvg  "Load Average [%.2f]" ...

This will keep the full internal precision but round it to the nearest hundredths for display.

To round the values when you store them you basically need to multiply by 100, round to the nearest integer, then device by 100.

import java.util.Math


    val float loadAvg = (float)Math::round((LoadAverage.state as DecimalType) * Math::pow(10, 2)) / Math::pow(10, 2)

This is right and therefore openHAB usually works with BigDecimals internally.

I would guess that if the systeminfo binding does not do this internally, so to get rid off these loooong log messages, it might also be a good idea to adapt fix the systeminfo binding accordingly. As we will soon have a new one in OH2 though, this might not be too relevant here anymore.

Thanks Kai…

Just heard your bit on the while running today. Nice work!

1 Like