Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way to add commas to an integer? [duplicate]

Possible Duplicate:
How to print number with commas as thousands separators?

For example:

>> print numberFormat(1234) >> 1,234 

Or is there a built-in function in Python that does this?

like image 403
ensnare Avatar asked Oct 11 '10 19:10

ensnare


People also ask

How do you add a comma to an integer in Python?

In Python, to format a number with commas we will use “{:,}” along with the format() function and it will add a comma to every thousand places starting from left. After writing the above code (python format number with commas), Ones you will print “numbers” then the output will appear as a “ 5,000,000”.

Can an integer have a comma?

Unfortunately Integer type fields do not have commas. You would need to use a Float type field for this. You should be able to change the type of your existing field in Field Management.

How do you put a comma in a string in Python?

''' if type(num) == int: return '{:,}'. format(num) elif type(num) == float: return '{:,. 2f}'. format(num) # Rounds to 2 decimal places else: print("Need int or float as input to function comma()!")


1 Answers

No one so far has mentioned the new ',' option which was added in version 2.7 to the Format Specification Mini-Language -- see PEP 378: Format Specifier for Thousands Separator in the What's New in Python 2.7 document. It's easy to use because you don't have to mess around with locale (but is limited for internationalization due to that, see the original PEP 378). It works with floats, ints, and decimals — and all the other formatting features provided for in the mini-language spec.

Sample usage:

print format(1234, ",d")    # -> 1,234 print "{:,d}".format(1234)  # -> 1,234 print(f'{1234:,d}')         # -> 1,234 (Python 3.6+) 

Note: While this new feature is definitely handy, it's actually not all that much harder to use the locale module, as several others have suggested. The advantage is that then numeric output can be made to automatically follow the proper thousands (and other) separator conventions used in various countries when outputting things like numbers, dates, and times. It's also very easy to put the default settings from your computer into effect without learning a bunch of language and country codes. All you need to do is:

import locale locale.setlocale(locale.LC_ALL, '')  # empty string for platform's default settings 

After doing that you can just use the generic 'n' type code for outputting numbers (both integer and float). Where I am, commas are used as the thousand separator, so after setting the locale as shown above, this is what would happen:

print format(1234, "n")    # -> 1,234 print "{:n}".format(1234)  # -> 1,234 

Much of the rest of the world uses periods instead of commas for this purpose, so setting the default locale in many locations (or explicitly specifying the code for such a region in a setlocale() call) produces the following:

print format(1234, "n")    # -> 1.234 print "{:n}".format(1234)  # -> 1.234 

Output based on the 'd' or ',d' formatting type specifier is unaffected by the use (or non-use) of setlocale(). However the 'd' specifier is affected if you instead use the locale.format() or locale.format_string() functions.

like image 127
martineau Avatar answered Sep 19 '22 07:09

martineau