python3 的 round 函式的 練習

weixin_33912246發表於2017-04-18

python3 的 round 函式感覺很彆扭,其運算結果與習慣不相符。特記錄下來:

程式碼


'''
python 3的 round 函式 是“四捨六入五成雙”的

https://www.zhihu.com/question/20128906
'''
print('python 3的 round 函式:四捨六入五成雙')

print('\nround(-3.5) = ', round(-3.5))

print('\nround(-2.5) = ', round(-2.5))
print('round(-1.5) = ', round(-1.5))

print('\nround(-0.5) = ', round(-0.5))
print('round(0.5) = ', round(0.5))

print('\nround(1.5) = ', round(1.5))
print('round(2.5) = ', round(2.5))

print('\nround(3.5) = ', round(3.5))
print('round(4.5) = ', round(4.5))

print('\nround(5.5) = ', round(5.5))
print('round(6.5) = ', round(6.5))


print('\nround(6.5000000000000001) = ', round(6.5000000000000001)) # 小數點後16位
print('round(6.500000000000001) = ', round(6.500000000000001)) # 小數點後15位

效果圖

709432-20170418232725696-143799197.jpg

相關文章