Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added function to convert number to indian comma system #11305

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions strings/indian_number_comma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def indian_number_comma(number: str) -> str:
"""
Returns the string with comma separated as per indian number system
:param number:
:return: number_with_comma
>>> indian_number_comma("6789")
6,789
>>> indian_number_comma("789")
789
>>> indian_number_comma("7980999")
79,80,999
"""
last_3_digits = number[-3:]
# store the numbers before 3 digits
before_3 = number[:-3]
# converts the string into of 2-2 characters
split_list = [before_3[i : i + 2] for i in range(0, len(before_3), 2)]
# add the separated last digits to the list
split_list.append(last_3_digits)
# join the whole list around ,
result = ",".join(split_list)
return result


if __name__ == "__main__":
assert indian_number_comma("6789"), "6,789"
assert indian_number_comma("789"), "789"
assert indian_number_comma("7980999"), "79,80,999"