0 python
这与这篇文章不同。
我正在努力在 Python 中将毫秒转换为这种格式“{分钟}:{秒}”。
我已经实现了这个转换
duration_in_ms = 350001
x = duration_in_ms / 1000
seconds = x % 60
x /= 60
'{}:{}'.format(str(int(x)).zfill(2), round(seconds,3))
Run Code Online (Sandbox Code Playgroud)
输出是
'05:50.001'
Run Code Online (Sandbox Code Playgroud)
有没有更有效的方法来做到这一点?
您可以使用 f 字符串:
duration = 350001
minutes, seconds = divmod(duration / 1000, 60)
f'{minutes:0>2.0f}:{seconds:.3f}'
Run Code Online (Sandbox Code Playgroud)
输出:
'05:50.001'
Run Code Online (Sandbox Code Playgroud)