Convert distance to Percent with two decimal numbers  [Solved]

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

Moderator: leecollings

Post Reply
besix
Posts: 99
Joined: Friday 25 January 2019 11:33
Target OS: Linux
Domoticz version: beta
Location: Poland
Contact:

Convert distance to Percent with two decimal numbers

Post by besix »

Hey
I have a simple script that converts the distance to a percentage but gives a percentage with a large decimal number. how to make only two decimal numbers

Code: Select all

local pct_sensor = 'Opał'
local depth_sensor = 'Dystans'
local tank_height = 100

return {
        on = {
                devices = {depth_sensor}
        },
        execute = function(dz, device)
		local depthdev = dz.devices(depth_sensor)
		local pctdev = dz.devices(pct_sensor)
		local depth = depthdev.state
		
		-- Calculate percentage 
		local pct = (tank_height - depth) / tank_height * 100 
		dz.log('Tank update to ' .. pct.. '% ' , dz.LOG_INFO)
		pctdev.updatePercentage(pct)
	
end
}
		 
I get 40.349456%
I want 40.35%
Who will help ?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Convert distance to Percent with two decimal numbers

Post by waaren »

besix wrote: Saturday 02 May 2020 10:11 I have a simple script that converts the distance to a percentage but gives a percentage with a large decimal number. how to make only two decimal numbers

I get 40.349456%
I want 40.35%
Changing line

Code: Select all

		local pct = (tank_height - depth) / tank_height * 100 
to

Code: Select all

               local pct = dz.utils.round((tank_height - depth) / tank_height * 100 ,2)
should do it.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
besix
Posts: 99
Joined: Friday 25 January 2019 11:33
Target OS: Linux
Domoticz version: beta
Location: Poland
Contact:

Re: Convert distance to Percent with two decimal numbers  [Solved]

Post by besix »

It works now, Thank you very much! Waaren
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest