Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string formatting
#1
When I run this simple program 0.166666666666 is printed.
What do i do to make the print 0.17
i.e. what is the formatting command to truncate the string to two digits beyond the dec point??


x = (1/6)
s = str(x)
print(s)
Reply
#2
You can use either format, or the formatting in f-strings. The description of it from the documentation is found in the Format specification mini-language.

>>> x = 1/6
>>> print(x)
0.16666666666666666
>>> print(format(x, ".2f"))
0.17
>>> print(f"{x:.2f}")
0.17
BashBedlam likes this post
Reply
#3
thanks
Reply
#4
oops, another issue. in the following I want to see 12.34. not 1.23+01
i.e.

x = 12.345678123456
s = format(x,".2")
print(s)
 
Reply
#5
print(f"{x: 2.3f}")
12.346
Reply
#6
Use f-string as shown last bye bowlofred.
>>> x = 12.345678123456
>>> print(f"The number is {x:.2f} a couple more {x:.4f}")
The number is 12.35 a couple more 12.3457
Reply
#7
in the following, is the 3 the digits to the right of the decimal?
what does the 2 mean?

print(f"{x: 2.3f}")


12.346What if my number was 123456.345 and I want to see 123456.3
Reply
#8
(Jan-02-2022, 01:06 AM)barryjo Wrote: 123456.345 and I want to see 123456.3
Then is n:.1f
>>> n = 123456.345 
>>> print(f"The number with one decimal place {n:.1f}")
The number with one decimal place 123456.3
>>> print(f"The number with ten decimal place {n:.10f}")
The number with 10 decimal place 123456.3450000000
f'{value:{width}.{precision}}'
width specifies the number of characters used in total to display.

Some more stuff with f-string
>>> n = 123456
>>> print(f"The number in binary {n:02b}")
The number in binary 11110001001000000
>>> print(f"The number in hex {n:02X}")
The number in hex 1E240
# f-strings can take any Python expressions inside the curly braces
>>> grapes = 5.20
>>> apples = 9.70
>>> print(f'The price is {apples + grapes:.2f}$')
The price is 14.90$

# Also methods eg .upper()
>>> for word in 'f-strings are cool'.split():
...     print(f'{word.upper():~^20}')
...
~~~~~F-STRINGS~~~~~~
~~~~~~~~ARE~~~~~~~~~
~~~~~~~~COOL~~~~~~~~
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting a date time string read from a csv file DosAtPython 5 1,303 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  String formatting (strptime) issues Henrio 2 853 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  confused about string formatting barryjo 7 2,009 Mar-06-2022, 02:03 AM
Last Post: snippsat
  Help with string formatting in classes brthurr 6 9,435 Dec-17-2021, 04:35 PM
Last Post: Jeff900
  Question on HTML formatting with set string in message Cknutson575 3 3,509 Mar-09-2021, 08:11 AM
Last Post: Cknutson575
  smtplib: string formatting not carrying over to email ClassicalSoul 1 2,665 Apr-22-2020, 09:58 PM
Last Post: bowlofred
  String formatting difficulties mmk1995 3 2,797 Aug-09-2019, 11:18 AM
Last Post: wavic
  string formatting Uchikago 1 1,934 Jun-28-2019, 03:28 PM
Last Post: buran
  TypeError: not all arguments converted during string formatting RedSkeleton007 1 14,981 Jul-15-2018, 08:51 PM
Last Post: ichabod801
  formatting string and returning as geojson garynobles 12 6,659 Mar-06-2018, 05:02 PM
Last Post: garynobles

Forum Jump:

User Panel Messages

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