Skip to content

Commit 5972a02

Browse files
committed
round float
1 parent b59b267 commit 5972a02

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

newcodes/answers/q66.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
class RoundFloat(object): #Python 3: class RoundFloat:
5+
def __init__(self, val):
6+
assert isinstance(val, float), "value must be a float."
7+
self.value = round(val, 2)
8+
9+
def __str__(self):
10+
return "{:.2f}".format(self.value)
11+
12+
__repr__ = __str__
13+
14+
if __name__ == "__main__":
15+
while True:
16+
n = input("Input a float:('q'-exit)")
17+
if n == 'q':
18+
print('See you next time.')
19+
break
20+
else:
21+
n = float(n)
22+
r = RoundFloat(n)
23+
print(r)

0 commit comments

Comments
 (0)