You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Wed Oct 7 14:43:36 2020
  5. @author: ljia
  6. """
  7. def rounder(x, decimals):
  8. """Round, where 5 is rounded up.
  9. Parameters
  10. ----------
  11. x : float
  12. The number to be rounded.
  13. decimals : int
  14. Decimals to which ``x'' is rounded.
  15. Returns
  16. -------
  17. string
  18. The rounded number.
  19. """
  20. x_strs = str(x).split('.')
  21. if len(x_strs) == 2:
  22. before = x_strs[0]
  23. after = x_strs[1]
  24. if len(after) > decimals:
  25. if int(after[decimals]) >= 5:
  26. after0s = ''
  27. for c in after:
  28. if c == '0':
  29. after0s += '0'
  30. elif c != '0':
  31. break
  32. if len(after0s) == decimals:
  33. after0s = after0s[:-1]
  34. after = after0s + str(int(after[0:decimals]) + 1)[-decimals:]
  35. else:
  36. after = after[0:decimals]
  37. elif len(after) < decimals:
  38. after += '0' * (decimals - len(after))
  39. return before + '.' + after
  40. elif len(x_strs) == 1:
  41. return x_strs[0]
  42. if __name__ == '__main__':
  43. x = 1.0075333616
  44. y = rounder(x, 2)
  45. print(y)

A Python package for graph kernels, graph edit distances and graph pre-image problem.