在python中对从数据库获取的数字进行四舍五入

2024-10-09 02:31:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个关于电报上公共汽车时间的项目,但是Python打印以0结束的小时,而不打印0,例如10.10变成10.1

if len(htp) > 0: # if the list contains at least one time
    alert3 = "I prossimi pullman sono alle: "
    orari_ufficiosi = []
    for rfd in htp:  # for each timetable
    if allhd[allh.index(str(rfd))] != "0": # if the destination stop has that line
        orari_ufficiosi.append(rfd)
        bot.api.call('sendMessage', {
                     'chat_id': chat_id,
                     'text': alert3 + str(",\n ".join(map(str,orari_ufficiosi)))
                     })

可以看出这个问题


Tags: the项目idforif时间chat电报
2条回答

我试过了,我想format(a,'.2f')会适合你的需要,或者你从下面选择。下面是一个如何使用的示例:

>>> a = 10.10
>>> a
10.1
>>> format(a,'.2f')
'10.10'
>>> format(a,'.3f')
'10.100'

但请注意,它将是一个字符串,我想你将发送一个电报消息,所以这将为你工作

检查此代码:

n = 10.10
print('The number is {number:.2f}'.format(number = n))

样本输出:

> The number is 10.10

相关问题 更多 >

    热门问题