Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def commas(x):
- x = round(x, 3)
- if x == round(x):
- x = int(x)
- sgn = x < 0
- if sgn:
- x = -x
- s = str(x)
- a = s.split('.')
- s = a[0]
- s = ''.join(reversed(list(s)))
- res = ''
- for i in range(len(s)):
- res += s[i]
- if i % 3 == 2 and i != len(s) - 1:
- res += ','
- res = ''.join(reversed(list(res)))
- if len(a) == 2:
- res += '.' + a[1]
- if sgn:
- res = '-' + res
- return res
- assert commas(1) == "1"
- assert commas(1000) == "1,000"
- assert commas(100.2346) == "100.235"
- assert commas(1000000000.23) == "1,000,000,000.23"
- assert commas(-999.9999) == "-1,000"
- assert commas(-1234567.0001236) == "-1,234,567"
- print("TEST PASSED")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement