Abdul Gandal
Abdul Gandal

Reputation: 97

Round a number in python get from a database

I'm working on a project about bus times on Telegram, but Python prints the hour which ends with 0 without the 0, e.g 10.10 becomes 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)))
                     })

The problem can be seen here.

Upvotes: 1

Views: 81

Answers (2)

JenilDave
JenilDave

Reputation: 604

I tried this I guess format(a,'.2f') will be correct one for your need or you select from below. Here's an example how to use:

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

But see to it that it will be a string and I guess you will be sending this as a Telegram message so this will work for you.

Upvotes: 2

Zephyr
Zephyr

Reputation: 12496

Check this code:

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

Sample output:

> The number is 10.10

Upvotes: 1

Related Questions