C语言 Lua - 数字到字符串的行为
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36031078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lua - Number to string behaviour
提问by Virus721
I would like to know how Lua handles the number to string conversions using the tostring()function.
我想知道 Lua 如何使用该tostring()函数处理数字到字符串的转换。
It is going to convert to an int (as string) if the number is round (i.e if number == (int) number) or is it always going to output a real (as string) like 10.0?
如果数字是圆的(即 if number == (int) number),它将转换为 int(作为字符串),或者它总是会输出一个实数(作为字符串),如10.0?
I need to mimic the exact behaviour of Lua's tostringin C, without using the Lua C API since, in this case, I'm not using a lua_State.
我需要tostring在 C 中模拟 Lua 的确切行为,而不使用 Lua C API,因为在这种情况下,我没有使用lua_State.
回答by Yu Hao
In Lua 5.2 or earlier, both tostring(10)and tostring(10.0)result as the string "10".
在 Lua 5.2 或更早版本中,tostring(10)andtostring(10.0)结果都是 string "10"。
In Lua 5.3, this has changed:
在 Lua 5.3 中,这已经改变了:
print(tostring(10)) -- "10"
print(tostring(10.0)) -- "10.0"
That's because Lua 5.3 introduced the integer subtype. From Changes in the Language:
那是因为 Lua 5.3 引入了整数子类型。从语言的变化:
The conversion of a float to a string now adds a
.0suffix to the result if it looks like an integer. (For instance, the float2.0will be printed as2.0, not as2.) You should always use an explicit format when you need a specific format for numbers.
.0如果结果看起来像整数,则浮点数到字符串的转换现在会为结果添加后缀。(例如,浮点数2.0将打印为2.0,而不是2。)当您需要数字的特定格式时,您应该始终使用显式格式。
回答by user2262111
If you are using 5.3.4 and you need a quick hotfix, use math.floor - it casts it to an int-number. This beats @warspyking answer in efficiency, but lacks the coolness that is bunches of code.
如果您使用的是 5.3.4 并且需要快速修补程序,请使用 math.floor - 它将其转换为整数。这在效率上击败了@warspyking 的答案,但缺乏大量代码的酷炫。
>tostring(math.floor(54.0))
54
>tostring(54.0)
54.0
>type(math.floor(54.0))
integer
>type(54.0)
number
回答by codingbunny
Lua converts the numbers as is:
Lua 按原样转换数字:
print(tostring(10)) => "10"
print(tostring(10.0)) => "10.0"
print(tostring(10.1)) => "10.1"
If you want to play around with them, there's a small online parser for simple commands like this : http://www.lua.org/cgi-bin/demoThis uses Lua 5.3.1
如果你想和他们一起玩,有一个小的在线解析器,用于像这样的简单命令:http: //www.lua.org/cgi-bin/demo这使用 Lua 5.3.1
editI must support Egor's comment, it's version dependent. I ran this locally on my system:
编辑我必须支持 Egor 的评论,它取决于版本。我在我的系统上本地运行这个:
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
> print(tostring(10))
10
> print(tostring(10.0))
10
回答by warspyking
In Lua 5.3, due to the integer type, tostringon a float (although it's Numeric value may be equivalent to an integer) will add a "'.0'suffix, but that doesn't mean you can't shorten it!
在 Lua 5.3 中,由于是整数类型,tostring对浮点数(虽然它的 Numeric 值可能等价于整数)会添加"'.0'后缀,但这并不意味着你不能缩短它!
local str = tostring(n)
if str:sub(-2) == ".0" then
str = str:sub(1,-3)
end

