Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

How to format a lua string with a boolean variable?

I have a boolean variable whose value I'd like to display in a formatted string. I tried using string.format, but get something like the following for any choice of format option listed in the language reference:

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print(string.format("%c
", true))
stdin:1: bad argument #2 to 'format' (number expected, got boolean)
stack traceback:
    [C]: in function 'format'
    stdin:1: in main chunk
    [C]: ?

I can get the boolean to display by adding a tostring,

> print(string.format("%s
", tostring(true)))
true

but that seems rather indirect to this lua beginner. Is there an formatting option I've overlooked? Or should I use the above approach? Something else?

question from:https://stackoverflow.com/questions/6615572/how-to-format-a-lua-string-with-a-boolean-variable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looking at the code of string.format, I don't see anything that supports boolean values. I guess tostring is the most reasonable option in that case.

Example:

print("this is: " .. tostring(true))  -- Prints:   this is true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...