Python Forum

Full Version: String formatting difficulties
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I would like to ask, why the below code is valid,

def place_value(number): 
	return ("{:,.2f}".format(number)) 

print(place_value(1000000.006)) 
But below code is not valid?

def place_value(number): 
	return ("{:,8.2f}".format(number)) 

print(place_value(1000000.006)) 
Thanks!
>>> >>> print("{:8,.2f}".format(1000000.006))
1,000,000.01
>>>
This should work. The comma must come after the 8.
'{:8,.2f}'
I haven't read the specification now, but you'll find it in the documentation.
First is the formatting for the number and then for the fraction.