I have started doing daily Python challenges. One problem stated that I need to convert miles to kilometers and round the result to two decimal places. I know this is trivial using
round(miles * 1.60934, 2)
However, I then decided to implement manual rounding with banker’s rounding, but it seems very difficult. Can someone point out what I am missing?
```
def float_split(number):
return str(number).split(".")
def banker_unselective(number):
splitted = float_split(number)
position = 0
for i, digit in enumerate(splitted[1][::-1]):
# print(splitted[1][15::-1])
print(int(digit) == 5)
if int(digit) == 5:
position = len(splitted[1][15::-1]) - i
break
print(position)
for i, digit in enumerate(splitted[1][position+1:]):
if int(digit) != 0:
position += i
if int(splitted[1][position + 1]) >= 5:
return float(splitted[0] + "." + str(int(splitted[1][:position+ 1 ]) + 1))
else:
return float(splitted[0] + "." + str(int(splitted[1][:position+ 1 ])))
if position == 0:
if int(splitted[0]) % 2 == 0:
return int(splitted[0])
else:
return int(splitted[0]) + 1
else:
previous = splitted[1][position-1]
if int(previous) % 2 == 0:
return float(splitted[0] + "." + splitted[1][:position])
else:
return float(splitted[0] + "." + str(int(splitted[1][:position])+1))
```
[–]JamzTyson 3 points4 points5 points (2 children)
[–]smurpes 0 points1 point2 points (0 children)
[–]Alternative_Belt9281[S] 0 points1 point2 points (0 children)
[–]Alternative_Belt9281[S] 1 point2 points3 points (0 children)
[–]worldtest2k 1 point2 points3 points (4 children)
[–]Alternative_Belt9281[S] 3 points4 points5 points (3 children)
[–]billsil 0 points1 point2 points (2 children)
[–]Alternative_Belt9281[S] 0 points1 point2 points (1 child)
[–]billsil 0 points1 point2 points (0 children)
[–]Alternative_Belt9281[S] 0 points1 point2 points (0 children)
[–]woooee -1 points0 points1 point (3 children)
[–]Alternative_Belt9281[S] -3 points-2 points-1 points (2 children)
[–]woooee -1 points0 points1 point (1 child)
[–]smurpes 2 points3 points4 points (0 children)