1. 程式人生 > 其它 >Python程式設計:毫秒數換算成常見的小時數、分鐘數、秒數

Python程式設計:毫秒數換算成常見的小時數、分鐘數、秒數

技術標籤:Pythonpython演算法

1.題目

請你隨便輸入一個毫秒數,快速換算成常見的小時數、分鐘數、秒數。

#  如何讓python計算結果保留兩位小數A:
#
# 設結果為a:
# 1)round(a,2)
# 2)’%.2f’ % a
# 3)Decimal(a).quantize(Decimal(‘0.00’))

2.程式碼展示

def TimeConverter():
    ms = int(input('請輸入毫秒數:'))
    # 保留兩位小數,但若ms太小,h就會顯示為0。
    s = round(ms / 1000, 2)
    m = round(s / 60, 2
) h = round(m / 60, 2) print('{0}換算後等於{1}秒,等於{2}分鐘,等於{3}小時'.format(ms, s, m, h)) TimeConverter()

在這裡插入圖片描述