Page 1 of 1

Trim variable

Posted: Saturday 02 April 2016 18:33
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

Re: Trim variable

Posted: Sunday 03 April 2016 19:51
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