我需要找出如何将数字格式化为字符串。我的代码在这里:

return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm

小时和分钟是整数,秒是浮点数。str() 函数会将所有这些数字转换为十分之一 (0.1) 位。因此,我的字符串不会输出“5:30:59.07 pm”,而是显示类似“5.0:30.0:59.1 pm”的内容。

最重要的是,我需要什么库/函数来为我执行此操作?

有帮助吗?

解决方案

从 Python 3.6 开始,Python 中的格式化可以使用 格式化字符串文字 或者 F 弦:

hours, minutes, seconds = 6, 56, 33
f'{hours:02}:{minutes:02}:{seconds:02} {"pm" if hours > 12 else "am"}'

或者 str.format 从2.7开始的功能:

"{:02}:{:02}:{:02} {}".format(hours, minutes, seconds, "pm" if hours > 12 else "am")

或者 字符串格式化 % 操作员 对于更旧版本的 Python,请参阅文档中的注释:

"%02d:%02d:%02d" % (hours, minutes, seconds)

对于您格式化时间的具体情况,有 time.strftime:

import time

t = (0, 0, 0, hours, minutes, seconds, 0, 0, 0)
time.strftime('%I:%M:%S %p', t)

其他提示

从 Python 2.6 开始,有一个替代方案:这 str.format() 方法。以下是使用现有字符串格式运算符的一些示例(%):

>>> "Name: %s, age: %d" % ('John', 35) 
'Name: John, age: 35' 
>>> i = 45 
>>> 'dec: %d/oct: %#o/hex: %#X' % (i, i, i) 
'dec: 45/oct: 055/hex: 0X2D' 
>>> "MM/DD/YY = %02d/%02d/%02d" % (12, 7, 41) 
'MM/DD/YY = 12/07/41' 
>>> 'Total with tax: $%.2f' % (13.00 * 1.0825) 
'Total with tax: $14.07' 
>>> d = {'web': 'user', 'page': 42} 
>>> 'http://xxx.yyy.zzz/%(web)s/%(page)d.html' % d 
'http://xxx.yyy.zzz/user/42.html' 

这是等效的片段,但使用 str.format():

>>> "Name: {0}, age: {1}".format('John', 35) 
'Name: John, age: 35' 
>>> i = 45 
>>> 'dec: {0}/oct: {0:#o}/hex: {0:#X}'.format(i) 
'dec: 45/oct: 0o55/hex: 0X2D' 
>>> "MM/DD/YY = {0:02d}/{1:02d}/{2:02d}".format(12, 7, 41) 
'MM/DD/YY = 12/07/41' 
>>> 'Total with tax: ${0:.2f}'.format(13.00 * 1.0825) 
'Total with tax: $14.07' 
>>> d = {'web': 'user', 'page': 42} 
>>> 'http://xxx.yyy.zzz/{web}/{page}.html'.format(**d) 
'http://xxx.yyy.zzz/user/42.html'

与 Python 2.6+ 一样,所有 Python 3 版本(到目前为止)都了解如何执行这两个操作。我无耻地直接把这些东西撕了出来 我的核心 Python 入门书 以及介绍+中级的幻灯片 我提供的Python课程 不时地。 :-)

2018 年 8 月更新: :当然,现在我们已经 3.6 中的 f 字符串功能, ,我们需要等效的例子 , ,是的,另一种选择:

>>> name, age = 'John', 35
>>> f'Name: {name}, age: {age}'
'Name: John, age: 35'

>>> i = 45
>>> f'dec: {i}/oct: {i:#o}/hex: {i:#X}'
'dec: 45/oct: 0o55/hex: 0X2D'

>>> m, d, y = 12, 7, 41
>>> f"MM/DD/YY = {m:02d}/{d:02d}/{y:02d}"
'MM/DD/YY = 12/07/41'

>>> f'Total with tax: ${13.00 * 1.0825:.2f}'
'Total with tax: $14.07'

>>> d = {'web': 'user', 'page': 42}
>>> f"http://xxx.yyy.zzz/{d['web']}/{d['page']}.html"
'http://xxx.yyy.zzz/user/42.html'

Python 2.6+

可以使用 format() 函数,所以在你的情况下你可以使用:

return '{:02d}:{:02d}:{:.2f} {}'.format(hours, minutes, seconds, ampm)

使用此功能的方法有多种,因此有关更多信息,您可以查看 文档.

Python 3.6+

f-strings 是 Python 3.6 中添加到该语言中的一项新功能。这非常有利于格式化字符串:

return f'{hours:02d}:{minutes:02d}:{seconds:.2f} {ampm}'

您可以使用 C 风格的字符串格式:

"%d:%d:d" % (hours, minutes, seconds)

请参阅此处,特别是: https://web.archive.org/web/20120415173443/http://diveintopython3.ep.io/strings.html

您可以使用以下来实现所需的功能

"%d:%d:d" % (hours, minutes, seconds)

字符串() 在Python中,整数将 不是 打印任何小数位。

如果您想忽略小数部分的浮点数,则可以使用 str(int(floatValue))。

也许下面的代码可以演示:

>>> str(5)
'5'
>>> int(8.7)
8

如果您有一个包含小数的值,但小数值可以忽略不计(即:100.0) 并尝试对其进行 int,您将收到错误。这看起来很愚蠢,但首先调用 float 可以解决这个问题。

str(int(浮点([变量])))

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top