I have a 2 bytes number from KNX and want to do some calculation (i.e. division) with a number from another system with higher precision (I guess about 4 bytes). I need to get same precision, i.e. round down the 4 bytes number to 2 bytes, otherwise I get some artifacts with low values.
Has anybody an idea how I can round a number to 2 bytes precision?
If you are doing this in Java than know that you are dealing with Java. In that case:
byte = 1 byte
short = 2 bytes
int = 4 bytes
long = 8 bytes
float = 4 bytes
double = 8 bytes
If the number you are getting from the other system is an integer type you should be able to just call .shortValue() and get a 2 byte version of the number (or an exception if the number doesn’t fit in 2 bytes, you can’t “round” 42000 into a short whose maximum value that can be represented is 32767).
However, if the number you are getting is a floating point number you will notice that there is no floating point number that uses less than 4 bytes. I’m not sure it is possible to to this at all and am skeptical that your description of the problem is correct and that KNX is expecting a 2 byte floating point number.
Thank you both for help. What I did in the end, was to specify the 2 byte knx datapoint as floatValue. This solved the issue, however, to be honest, I don’t fully understand why. I have no clear view on the effects of the various data types.