function toboolean()

From wiki.netio-products.com
Revision as of 18:36, 18 April 2017 by Jturon (talk | contribs) (Založena nová stránka s textem „{{DISPLAYTITLE:function toboolean()}} Converts variable to boolean (true or false) value (the only type usable in conditions). <i>boolean</i> toboolean(<…“)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Converts variable to boolean (true or false) value (the only type usable in conditions).

boolean toboolean(variable)

Parameters

mixed variable will be converted to boolean value.

Return value

Falsy values are:

  • number 0
  • boolean false
  • string ""
  • table {}

Anything else converts to true.

Usage

local answer = 42
if toboolean(answer) then
  log("42 is true!")
else
  log("42 is false!")
end

To use inverse conversion (boolean to number) use 'x and 1 or 0' idiom:

local x = true
logf("x is %d", x and 1 or 0)

See also

Lua system's tonumber() and tostring() functions.