Python Forum
String formatting difficulties - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: General Coding Help (https://python-forum.io/Forum-General-Coding-Help)
+--- Thread: String formatting difficulties (/Thread-String-formatting-difficulties)



String formatting difficulties - mmk1995 - Aug-09-2019

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!


RE: String formatting difficulties - wavic - Aug-09-2019

>>> >>> print("{:8,.2f}".format(1000000.006))
1,000,000.01
>>>



RE: String formatting difficulties - DeaD_EyE - Aug-09-2019

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.


RE: String formatting difficulties - wavic - Aug-09-2019

First is the formatting for the number and then for the fraction.