Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to write correctly ordered ( 23,45,212 ) comma separated numbers in python ? not the thousand separator

number = int(input())
# input: 2345212
print(f"{number:,}") # using inbuilt python's separator

# output:  
# Giving this : 2,345,212
# desired output: 23,45,212

I wish to generate correctly formatted numbers as output. But, I am not able to figure it out. As in desired output ( 23,45,212 ) from the last, it has a comma after 3 places thereafter a comma is placed on passing 2 digits. In India, it’s the standard form to place commas to digits.

Looking for the shortest trick.

[ Already tried locale. I want to print without currency( not this: ₹23,45,212.00 ) ]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do something like this using the built-in locale library:

import locale
number = int(input())

locale.setlocale(locale.LC_MONETARY, 'en_IN')
print(locale.currency(number, grouping=True))

Or without the currency symbol:

import locale
number = int(input())

locale.setlocale(locale.LC_MONETARY, 'en_IN')
print(locale.currency(number, grouping=True)[1:])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading