【问题标题】:Round a number in python get from a database从数据库中获取python中的数字
【发布时间】:2020-09-26 09:59:47
【问题描述】:

我正在 Telegram 上做一个关于公交车时间的项目,但 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)))
                     })

问题可见here。

【问题讨论】:

    标签: python string format rounding


    【解决方案1】:

    我试过了,我猜format(a,'.2f') 将是您需要的正确选项,或者您可以从下面选择。这是一个如何使用的示例:

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

    但请注意它是一个字符串,我猜你会将它作为 Telegram 消息发送,所以这对你有用。

    【讨论】:

      【解决方案2】:

      检查此代码:

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

      样本输出:

      > The number is 10.10
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-10
        • 1970-01-01
        • 2019-08-09
        • 2014-10-18
        • 2018-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多