Trim variable

Moderator: leecollings

Post Reply
CalderX
Posts: 1
Joined: Wednesday 09 September 2015 21:35
Target OS: Windows
Domoticz version: 2022.1
Location: Sweden
Contact:

Trim variable

Post by CalderX »

Hi!

I'm using this LUA-script for updating a textfile with temperature from one of my temperaturedevices.
Ii is working but the temperaturevalue has to many decimales, like 2.2999999523163

Is it possible to trim or round the value to maybe 1 decimales, like 2.3

My script:

commandArray = {}
if (devicechanged['Temp Ute_Temperature']) then
temp = devicechanged['Temp Ute_Temperature']
local file = io.open("C:\\Temperature\\temperature.txt", "w")
file:write(temp)
file:close()
end
return commandArray

Greetings

Calle
georgesattali
Posts: 84
Joined: Saturday 05 March 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Trim variable

Post by georgesattali »

google is your friend : http://lua-users.org/wiki/SimpleRound

The following function rounds a number to the given number of decimal places.
function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end

Bye, GD
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest