tostring

Converts its argument to a string

Prototype

s = tostring (a)

Description

Converts its argument to a string in a reasonable format. If a __tostring metatable field is found, that is used for the conversion.

print (tostring (print)) --> function: 0205A980
print (tostring (_G)) --> table: 02072780
print (tostring (1.23e10)) --> 12300000000

-- use __tostring to print a table

t = { age = 42, height = 102 }

setmetatable (t, 
  { 
  __tostring = function (t) 
    return "person of age " .. t.age
    end -- __tostring 
  }
 )

print (t) --> person of age 42

Also see bit.tostring which will convert a number to a string of any base in the range 2 to 36.

Lua functions

Topics