Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 String formatting difficulties
Hi all,

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

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

But below code is not valid?

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

Yoriz wrote Aug-09-2019, 11:34 AM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
>>> >>> print("{:8,.2f}".format(1000000.006))
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
This should work. The comma must come after the 8.
I haven't read the specification now, but you'll find it in the documentation.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
First is the formatting for the number and then for the fraction.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  smtplib: string formatting not carrying over to email ClassicalSoul 1 155 Apr-22-2020, 09:58 PM
Last Post: bowlofred
  difficulties to chage json data structure using json module in python Sibdar 1 310 Apr-03-2020, 06:47 PM
Last Post: micseydel
  string formatting Uchikago 1 472 Jun-28-2019, 03:28 PM
Last Post: buran
  TypeError: not all arguments converted during string formatting RedSkeleton007 1 11,015 Jul-15-2018, 08:51 PM
Last Post: ichabod801
  formatting string and returning as geojson garynobles 12 2,583 Mar-06-2018, 05:02 PM
Last Post: garynobles
  Dynamic Formatting of String mikera1979 2 936 Feb-27-2018, 07:09 AM
Last Post: mikera1979
  I have difficulties to use Pyshark jeanapala 1 905 Nov-21-2017, 01:45 AM
Last Post: Larz60+
  python2 string formatting - old and new - different for unicode buran 1 1,394 May-16-2017, 10:13 PM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)