Advertisement
here2share

# b_round_float.py

Feb 6th, 2020
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. # b_round_float.py
  2.  
  3. n = [round(123456.789, -(i-3)) for i in xrange(10)]
  4. for i in n: print i
  5.  
  6. '''
  7. output:
  8.  
  9. 123456.789
  10. 123456.79
  11. 123456.8
  12. 123457.0
  13. 123460.0
  14. 123500.0
  15. 123000.0
  16. 120000.0
  17. 100000.0
  18. 0.0
  19. '''
  20.  
  21. print
  22.  
  23. def f(z): return str(abs(i))
  24. x = '-123456789'
  25. t = [float(x[i:]) for i in xrange(len(x))] ### as floats just for testing
  26. n = [float(('-' if i<0 else '')+f(i)[0]+'0'*(len(f(i))-3)) for i in t]
  27. for i in n: print i
  28.  
  29. '''
  30. output:
  31.  
  32. -100000000.0
  33. 100000000.0
  34. 20000000.0
  35. 3000000.0
  36. 400000.0
  37. 50000.0
  38. 6000.0
  39. 700.0
  40. 80.0
  41. 9.0
  42. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement