Why are value converted to floats in a strange way?  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Why are value converted to floats in a strange way?

Post by EddyG »

It seems that value's are converted to floats in a strange way.

Code: Select all

Woonkamer Temperatuur: 21.200000762939
This value is in the database and display 21.2
Why is that?
ronaldbro
Posts: 327
Joined: Thursday 15 November 2018 21:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Why are value converted to floats in a strange way?

Post by ronaldbro »

That's because of the precision of the data type. You will see the same in Excel when you show a lot of decimals.

You can use string.format to specify how many decimals you want.
Try string.format("%.0f", 21.200000762939)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Why are value converted to floats in a strange way?

Post by waaren »

EddyG wrote: Sunday 07 June 2020 15:26 It seems that value's are converted to floats in a strange way.

Code: Select all

Woonkamer Temperatuur: 21.200000762939
This value is in the database and display 21.2
Why is that?
Short explanation:
because of the way the underlying platform handles floating point numbers, and ultimately with the inaccuracy you’ll always have when writing down numbers as a string of a fixed number of digits.
Long explanation
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Why are value converted to floats in a strange way?  [Solved]

Post by EddyG »

Tnx, That explains a lot. :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest