-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path010-round.py
More file actions
37 lines (32 loc) · 1.26 KB
/
Copy path010-round.py
File metadata and controls
37 lines (32 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
Round a number
--------------
Input (float) A floating point number
(int) Number of decimals
Default value is: 0
Output (float) Rounded number
(int) Whether using the default decimals value, the return number
will be the nearest integer
"""
number = 103.14159
# Rounding with 2 decimals
number_rounded = round(number, 2)
print('Rounding with 2 decimals')
print('original number: {}, rounded: {}, type of rounded: {}'
.format(number, number_rounded, type(number_rounded)))
# Rounding with -2 decimals
number_rounded = round(number, -2)
print('\nRounding with -2 decimals')
print('original number: {}, rounded: {}, type of rounded: {}'
.format(number, number_rounded, type(number_rounded)))
# Rounding with 0 decimals
number_rounded = round(number, 0)
print('\nRounding with 0 decimals')
print('original number: {}, rounded: {}, type of rounded: {}'
.format(number, number_rounded, type(number_rounded)))
# Rounding with default
# Result will be integer (!)
number_rounded = round(number)
print('\nRounding with default')
print('original number: {}, rounded: {}, type of rounded: {}'
.format(number, number_rounded, type(number_rounded)))