lua - 如何使用 bool 变量格式化lua字符串?

标签 lua string-formatting

我有一个 bool 变量,我想以格式化字符串显示其值。我尝试使用 string.format,但是对于 language reference 中列出的任何格式选项选择,都会得到类似以下内容的内容:

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

我可以通过添加tostring来显示 bool 值,

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

但这对于这个 lua 初学者来说似乎相当间接。是否有我忽略的格式选项?或者我应该使用上面的方法?还有别的吗?

最佳答案

查看string.format的代码,我没有看到任何支持 bool 值的内容。 我认为在这种情况下 tostring 是最合理的选择。

示例:

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

关于lua - 如何使用 bool 变量格式化lua字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6615572/

相关文章:

string - MSN 天气服务的 Lua string.match 模式

在Lua中POST数据

python - 用python3打印字符串格式: print from unpacked array *some* of the time

java - 将货币转换为 Double 时 String 类错误

Go:如何以可读的方式格式化这个结构?

lua - Lua中的表内表

c - Lua问题-运行前加载库文件

python - 使用 Aerospike Python 客户端 udf 从 Aerospike 中的一组记录中删除多个 bin

python - 格式化字符串与连接

c# - 使用字符串格式显示最多 2 位的小数或简单整数