我需要浮点数的特定格式:(符号)xx.dd当尝试为此格式设置string.format时,我得到奇怪的结果 .
h= 5.127 --(it should beconverted to +05.13)
print(string.format("%+05.2f",h))
--> 05.13
print(string.format("%+06.2f",h))
--> 005.13
h= -5.127 --(it should beconverted to -05.13)
print(string.format("%05.2f",h))
--> -5.13
print(string.format("%06.2f",h))
--> 0-5.13
当然,我有一个简单的解决方法,但我认为这个版本有问题 .
build在2018-04-09 15:12创建,由SDK 2.2.1上的Lua 5.1.4提供(cfd48f3)
BR,eHc
1 回答
这是NodeMCU中的错误(或未记录的缺陷) .
Lua通过将它们交给C标准库的
sprintf
函数来实现string.format
格式说明符的大部分处理 . (有一些事情sprintf
允许Lua没有,但+
应该工作正常 . )NodeMCU已经修改了Lua来替换大多数(或所有)标准库调用,并调用NodeMCU定义的替换函数(这通常是疯狂的,但在嵌入式系统域中可能没问题) . NodeMCU的
sprintf
实现不支持+
.这是来自NodeMCU源(c_stdio.c)的相关代码 . 请注意,格式说明符中的未知字符会被忽略:
同样,当前没有为数字实现
0
格式化 - 正如您所注意到的,它只是在左侧填充而不管符号 .