How to "logical and" a float within sensor filters?

Hi,
If I was dealing with a binary value, I would probably do something like:

- lambda: |-
   x && 0xF0;
   return x;

What would I do to truncate a float after decimal 2 without altering the remainder?
x = 123.456789
truncate after digit 5
x=123.45

Regards, Martin

Not entirely sure what you mean. I assume that you want to round down, not to nearest value but just limit the nr of decimals?
If so, can use this trick: x = (int)(x * 100) / 100.0

Thank you for taking the time to help.
Can you help a bit more and talk me through how this is working?
My understanding is that (int) turns x into an integer, then it is multiplied by 100, then divided by 100.
Lastly, the integer is “returned” to the sensor - a float. So is this just a “cast”: float > int > float?
This is my full lambda - just in case the context is making a difference!

- lambda: |-
            x = (int)(x * 100) / 100.0;
            ESP_LOGI("myDiagString", "The value of x is: %.5f", x);
            return x; 

This indeed will turn 123.456 into 123.45 (two decimal places). But how do I extend this to strip to one (or three) decimal place?
Change the 100’s to 10’s? No. That still results in 123.456 returning 123.45?

Regards, M.

No, x is first multiplied by 100, then turned into an int, and then divided by 100.0
To do the same with three digits you have to use 1000’s.

2 Likes