将一个数字翻倍

我得到了一个以秒为单位返回的数字,我想知道是否有可能从这个数字中得到双倍并添加到字符串部分?这是代码:

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)

点赞
用户2634887
用户2634887
`min = string.gsub(min, ".", ":")`

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

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

但是这仍然会给您带来分钟的小数部分,而不是秒数。尽管您说您想要 `3:63`,但我怀疑这不是真正需要的时间。

尝试:

`print(string.format("%d:%d", math.floor(time/60), time%60))`
2013-08-16 15:00:20
用户1022729
用户1022729

你可以使用 math.modf 函数。

time = os.time()
--get minutes and fractions 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))

翻译:

你可以使用 math.modf 函数。

time = os.time()
--获取分钟数和百分数分钟
minutes, seconds = math.modf(time/60)
--将分钟的百分数转换为秒
seconds = math.floor((seconds * 60) + 0.5)

--以 printf 的方式输出所有结果 :)
print(string.format("你获得了 %d:%d 分钟!", minutes, seconds))
2013-08-16 15:01:24
用户1560049
用户1560049

尝试并且操作 os.date:

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

08:20

2013-08-16 19:47:01