Missing type for str.format () behavior

According to Python docs here , when exiting a type, it defaults to using the type β€œg” for floating point arguments.

However

print("{0:.2}".format(14.9))

prints "1.5e + 01", and

print("{0:.2g}".format(14.9))

prints "15"

Is this just a problem with the wrong documentation or is there another reason for this?

+3
source share
2 answers

According to the source code, this is a documentation error. A correct description of behavior without a floating point specifier is "like" g ", but always with at least one digit after the decimal point."

+5
source

Python 2.7, Python 3.x. Python 3.x .

Python 2.7 :

>>> "{0:.2}".format(14.9)
'15.0'

>>> "{0:.2g}".format(14.9)
'15'
+1

Source: https://habr.com/ru/post/1791139/


All Articles