1

我得到了一个在几秒钟内返回的数字,我想知道有没有办法从这个数字中获取双倍并将其添加到字符串部分?这是代码:

local time = os.time() - LastTime()
local min = time / 60
min = tostring(min)
min = string.gsub(min, ".", ":")
print("You got "..min.." min!")

上面的文件返回: You got :::: min!

我正在寻找的只是将秒转换为分钟和秒(更像是 2:23)

4

3 回答 3

2

min = string.gsub(min, ".", ":")

此代码用冒号替换所有字符。这是因为您的第二个参数是正则表达式,其中句点匹配任何字符。你可以尝试用反斜杠转义它,即

min = string.gsub(min, "%.", ":")

然而,这仍然会给你分钟的分数,而不是秒数。尽管你说你想要3:63,但我怀疑情况是否如此,因为这是一个无效的时间。

尝试:

print(string.format("%d:%d", math.floor(time/60), time%60))

于 2013-08-16T15:00:20.453 回答
1

你可以使用math.modf函数。

time = os.time()
--get minutes and franctions of minutes
minutes, seconds = math.modf(time/60)
--change fraction of a minute to seconds
seconds = math.floor((seconds * 60) + 0.5)

--print everything in a printf-like way :)
print(string.format("You got %d:%d min!", minutes, seconds))
于 2013-08-16T15:01:24.163 回答
1

尝试使用 os.date:

print(os.date("%M:%S",500))

08:20

于 2013-08-16T19:47:01.433 回答