Bottom Page

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!
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.
Quote
#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
Quote
#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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  string formatting Uchikago 1 268 Jun-28-2019, 03:28 PM
Last Post: buran
  TypeError: not all arguments converted during string formatting RedSkeleton007 1 6,939 Jul-15-2018, 08:51 PM
Last Post: ichabod801
  formatting string and returning as geojson garynobles 12 2,034 Mar-06-2018, 05:02 PM
Last Post: garynobles
  Dynamic Formatting of String mikera1979 2 724 Feb-27-2018, 07:09 AM
Last Post: mikera1979
  I have difficulties to use Pyshark jeanapala 1 693 Nov-21-2017, 01:45 AM
Last Post: Larz60+
  python2 string formatting - old and new - different for unicode buran 1 1,199 May-16-2017, 10:13 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)