import math
print(f'對 2.675 向上取整:{math.ceil(2.675)}')
print(f'對 2.135 向上取整:{math.ceil(2.135)}')
print(f'對 2.675 向下取整:{math.floor(2.675)}')
print(f'對 2.135 向下取整:{math.floor(2.135)}')
2. 使用 round 函數對浮點數進行不精確四捨五入print(f'使用 round 函數對 2.675 取小數點後 2 位:{round(2.675,2)}')
print(f'使用 round 函數對 2.135 取小數點後 2 位:{round(2.135,2)}')
3. 使用 decimal 庫對浮點數進行精確四捨五入「強烈推薦此方法」from decimal import *
x = Decimal('2.675')
y = Decimal('2.135')
print('使用 decimal 庫的 ROUND_HALF_UP 參數對 2.675 取小數點後 2 位:{}'.format(x.quantize(Decimal('1.00'),rounding=ROUND_HALF_UP)))
print('使用 decimal 庫的 ROUND_HALF_UP 參數對 2.135 取小數點後 2 位:{}'.format(y.quantize(Decimal('1.00'),rounding=ROUND_HALF_UP)))
4. decimal 庫 quantize() 方法的 rounding 參數❝「ROUND_CEILING:捨入方向為正無窮」
「ROUND_DOWN :捨入方向為零」
「ROUND_FLOOR :捨入方向為負無窮」
「ROUND_HALF_DOWN :捨入到最接近的數,同樣接近則捨入方向為零」
「ROUND_HALF_EVEN :捨入到最接近的數,同樣接近則捨入到最接近的偶數(默認值)」
「ROUND_HALF_UP :捨入到最接近的數,同樣接近則捨入到零的反方向」
「ROUND_UP :捨入到零的反方向」
「ROUND_05UP :如果最後一位朝零的方向捨入後為 0 或 5 則捨入到零的反方向;否則捨入方向為零」
❞print('使用 decimal 庫的 ROUND_CEILING 參數對 2.675 取小數點後 2 位:{}'.format(x.quantize(Decimal('1.00'),rounding=ROUND_CEILING)))
print('使用 decimal 庫的 ROUND_CEILING 參數對 2.135 取小數點後 2 位:{}'.format(y.quantize(Decimal('1.00'),rounding=ROUND_CEILING)))
print('使用 decimal 庫的 ROUND_DOWN 參數對 2.675 取小數點後 2 位:{}'.format(x.quantize(Decimal('1.00'),rounding=ROUND_DOWN)))
print('使用 decimal 庫的 ROUND_DOWN 參數對 2.135 取小數點後 2 位:{}'.format(y.quantize(Decimal('1.00'),rounding=ROUND_DOWN)))
print('使用 decimal 庫的 ROUND_FLOOR 參數對 2.675 取小數點後 2 位:{}'.format(x.quantize(Decimal('1.00'),rounding=ROUND_FLOOR)))
print('使用 decimal 庫的 ROUND_FLOOR 參數對 2.135 取小數點後 2 位:{}'.format(y.quantize(Decimal('1.00'),rounding=ROUND_FLOOR)))
print('使用 decimal 庫的 ROUND_HALF_DOWN 參數對 2.675 取小數點後 2 位:{}'.format(x.quantize(Decimal('1.00'),rounding=ROUND_HALF_DOWN)))
print('使用 decimal 庫的 ROUND_HALF_DOWN 參數對 2.135 取小數點後 2 位:{}'.format(y.quantize(Decimal('1.00'),rounding=ROUND_HALF_DOWN)))
print('使用 decimal 庫的 ROUND_HALF_EVEN 參數對 2.675 取小數點後 2 位:{}'.format(x.quantize(Decimal('1.00'),rounding=ROUND_HALF_EVEN)))
print('使用 decimal 庫的 ROUND_HALF_EVEN 參數對 2.135 取小數點後 2 位:{}'.format(y.quantize(Decimal('1.00'),rounding=ROUND_HALF_EVEN)))
print('使用 decimal 庫的 ROUND_UP 參數對 2.675 取小數點後 2 位:{}'.format(x.quantize(Decimal('1.00'),rounding=ROUND_UP)))
print('使用 decimal 庫的 ROUND_UP 參數對 2.135 取小數點後 2 位:{}'.format(y.quantize(Decimal('1.00'),rounding=ROUND_UP)))
print('使用 decimal 庫的 ROUND_05UP 參數對 2.675 取小數點後 2 位:{}'.format(x.quantize(Decimal('1.00'),rounding=ROUND_05UP)))
print('使用 decimal 庫的 ROUND_05UP 參數對 2.135 取小數點後 2 位:{}'.format(y.quantize(Decimal('1.00'),rounding=ROUND_05UP)))5. 使用 format 進行指定位數截斷
print('保留 2.675 小數點後 2 位:{:.2f}'.format(2.675))
print('保留 2.135 小數點後 2 位:{:.2f}'.format(2.135))
6. 使用 / 進行除法print('對 2.675 進行除法:{}'.format(2.675 / 2))
print('對 2.135 進行除法:{}'.format(2.135 / 2))
7. 使用 // 進行整除print('對 2.675 進行整除:{}'.format(int(2.675 // 2)))
print('對 2.135 進行整除:{}'.format(int(2.135 // 2)))
8. 使用 % 進行取餘print('對 2.675 進行取餘:{:.2f}'.format(2.675 % 2))
9. 代碼下載「Github 項目地址」:https://github.com/don2vito/wechat_project.git「Gitee 項目地址」:https://gitee.com/don2vito/wechat_official_account.git歡迎關注
print('對 2.135 進行取餘:{:.2f}'.format(2.135 % 2))