PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
print('Rounding 4806.5 to ones place with default rounding strategy.')
print(round(4806.5, 0)) # expecting 4807
print()
import decimal
from decimal import Decimal
pos_num = Decimal('4806.5')
neg_num = Decimal('-4806.5')
print('Default decimal settings')
print(decimal.getcontext())
print('\nRounding both positive and negative numbers.')
print(f'From {pos_num}', 'result:', round(pos_num, 0)) # 4807
print(f'From {neg_num}', 'result:', round(neg_num, 0)) # -4807
# change rounding strategy to ROUND_HALF_UP
decimal.getcontext().rounding=decimal.ROUND_HALF_UP
print('\n\nAfter changing round strategy')
print(decimal.getcontext())
print('\nRounding both positive and negative numbers.')
print(f'From {pos_num}', 'result:', round(pos_num, 0)) # 4807
print(f'From {neg_num}', 'result:', round(neg_num, 0)) # -4807
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run