Python Forum
String formatting difficulties
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String formatting difficulties
#1
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!
Reply
#2
>>> >>> print("{:8,.2f}".format(1000000.006))
1,000,000.01
>>>
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
First is the formatting for the number and then for the fraction.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting a date time string read from a csv file DosAtPython 5 1,249 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  String formatting (strptime) issues Henrio 2 839 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  confused about string formatting barryjo 7 1,977 Mar-06-2022, 02:03 AM
Last Post: snippsat
  string formatting barryjo 7 2,036 Jan-02-2022, 02:08 AM
Last Post: snippsat
  Help with string formatting in classes brthurr 6 8,949 Dec-17-2021, 04:35 PM
Last Post: Jeff900
  Question on HTML formatting with set string in message Cknutson575 3 3,459 Mar-09-2021, 08:11 AM
Last Post: Cknutson575
  smtplib: string formatting not carrying over to email ClassicalSoul 1 2,643 Apr-22-2020, 09:58 PM
Last Post: bowlofred
  difficulties to chage json data structure using json module in python Sibdar 1 2,078 Apr-03-2020, 06:47 PM
Last Post: micseydel
  string formatting Uchikago 1 1,922 Jun-28-2019, 03:28 PM
Last Post: buran
  TypeError: not all arguments converted during string formatting RedSkeleton007 1 14,964 Jul-15-2018, 08:51 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020