Optimesh
Optimesh

Reputation: 2857

Is there a way to format a number with commas for thousands, without converting the int to a string?

I'd like to add commas to 'separate the thousands' when long integers are printed. The catch is, I don't want to convert the int to a string. There are a few questions in SO that suggest something like:

a = 1234567
print "The number is %s but it's converted to a string" %'{:,d}'.format(a)

====> The number is 1,342,424,242 but it's converted to a string

I was looking for a solution that wouldn't necessitate the conversion to a string. For example, when I wanted to display a number as a percent:

b = 0.1234
print "The Norwegian Blue prefers kippin' on it's back! %0.2f%%" % (b*100)

====> The Norwegian Blue prefers kippin' on it's back! 12.34%

Anyway to do something similar, only with the commas separation? Thanks!

EDIT: To better explain myself, I mean present the number with the commas, similarly to the way you present the percentage with a % mark and only 2 decimals after the point. You don't change the number, or transform it to a string, only manipulate the way it's displayed / presented. That's the sort of thing I'm trying to achieve.

EDIT2: I don't understand the negative sentiment here. I gave one way to go about something and asked if it could be done in a different way, that would be more similar to an example I gave. I arrived at both these examples after searching SO quite a bit. May be a noob question, but at least I had a go at it alone and looked for solutions before addressing the community. Not sure what I did wrong other than not knowing the answers. Tnx for reading.

Upvotes: 0

Views: 10299

Answers (2)

dawg
dawg

Reputation: 104092

To be clear, a itself is not being changed into a string. There is a temporary, anonymous string object created by '{:,d}'.format(a) and then fed to "%s" %:

>>> a = 1234567
>>> "The number is %s but it's converted to a string" %'{:,d}'.format(a)
"The number is 1,234,567 but it's converted to a string"
>>> type(a)
<type 'int'>   
>>> a
1234567

So a itself has not changed at all similarly as your second example does not change the object b:

>>> b = 0.1234
>>> "The Norwegian Blue prefers kippin' on it's back! %0.2f%%" % (b*100)
"The Norwegian Blue prefers kippin' on it's back! 12.34%"
>>> type(b)
<type 'float'>
>>> b
0.1234

So the underlying values of a and b are not changing. The issue is that you are using a less than clear syntax to print a. Just do:

>>> "The number is {:,d} but it's NOT converted to a string".format(a)
"The number is 1,234,567 but it's NOT converted to a string"

There is no need to do two formatting steps as you have in your example.

Upvotes: 1

RedX
RedX

Reputation: 15184

Your own example is able to do what you want. Just write it properly without mixing format styles:

a = 1234567
print "The number is {:,d} but it's converted to a string".format(a)

That's a good reason to use the modern syntax. It does not care about the type of the argument. It will use str on the object and output it accordingly.

print "The number is {:,d} and John has {} sisters and {} brothers".format(1000000, 2, 3)

Upvotes: 9

Related Questions