示例工程 11-formatting-and-bytes

11-formatting-and-bytes/01-format-specifiers/main.lua

lua · 0.5 KB

-- @runtimes lua54 luajit
--- 演示:格式说明符与 %q/%x/%g。
local M = {}
 
--- @return table { hex = string, float = string, padded = string }
function M.run(log)
	log = log or print
	local hex = string.format("%04x", 255)
	local float = string.format("%.2f", 3.14159)
	local padded = string.format("%8s", "hi")
	log(("hex=%s"):format(hex))
	log(("float=%s"):format(float))
	log(("padded=%q"):format(padded))
	return { hex = hex, float = float, padded = padded }
end
 
if not ... then
	M.run()
end
return M