简体   繁体   English

在浮动上使用f字符串格式打印-无法获得输出的生成方式

[英]print with f-string formatting on float - unable to get how the output is generated

x = 100.1205
y = str(x)[6]

which means y=0 这意味着y = 0

When try to execute below code output is 100. 当尝试执行以下代码时,输​​出为100。

print ("{:.{}f}".format(x,y))

Can anyone tell how it is giving 100 as output. 谁能说出它是如何输出100的。

First off, let's number the fields: the format string '{:.{}f}' is equivalent to '{0:.{1}f}' . 首先,让我们为字段编号:格式字符串'{:.{}f}'等同于'{0:.{1}f}'

The argument at position 1 is y , which equals 0, so this is equivalent to '{0:.0f}' by substitution. 位置1的自变量y等于0,因此它通过替换等效于'{0:.0f}' That is, it formats the argument at position 0 (ie x ) as a float, with 0 decimal places. 即,它将格式为0的参数(即x )格式化为浮点数,并保留小数点后0位。

So, the result is '100' , because that's x to 0 decimal places. 因此,结果是'100' ,因为那是x到0的小数位。 You can try this with different values of y to see the results: 您可以尝试使用不同的y值来查看结果:

>>> '{:.{}f}'.format(100.1205, 0)
'100'
>>> '{:.{}f}'.format(100.1205, 1)
'100.1'
>>> '{:.{}f}'.format(100.1205, 2)
'100.12'
>>> '{:.{}f}'.format(100.1205, 3)
'100.121'
>>> '{:.{}f}'.format(100.1205, 4)
'100.1205'
>>> '{:.{}f}'.format(100.1205, 5)
'100.12050'

The curly braces are being interpreted from the outside in, ie the outer-most braces will be replaced with the first argument of format() , ie x , while the inner-most braces will be replaced with the second argument of format() , ie y . 花括号从外部开始解释,即最外面的括号将被format()的第一个参数format()x format()替换,而最里面的括号将被format()的第二个参数替换,即y Here, x will represent the float to display, and y will represent the significant digits after the decimal place. 在此, x表示要显示的浮点数,y表示小数点后的有效数字。 See plenty of examples and docs here 在此处查看大量示例和文档

If this is still unclear, I would break it down into single format operation first, to better understand: 如果仍然不清楚,我将首先将其分解为单一格式操作,以更好地理解:

>>> print ("{}".format(x))
100.1205

Now all we want to do is trim off all significant digits after the decimal point, then: 现在我们要做的就是将小数点后的所有有效数字都修剪掉,然后:

>>> print ("{:.0f}".format(x))
100

Or to trim off all but one: 或修剪掉除一个以外的所有内容:

>>> print ("{:.1f}".format(x))
100.1

Adding another parameter to format() , ie y, then allows you to modify the significant digits with the value of y. format()添加另一个参数format()即y),然后允许您使用y的值修改有效数字。

>>> print ("{:.{}f}".format(x,y)
100

x = 100.1205, y = str(x)[6 ]. x = 100.1205, y = str(x)[6 ]。 here y = 0 这里y = 0

first of all understand the "{:.{}f}".format(x,y) . 首先了解"{:.{}f}".format(x,y) here we are using the position formatting of the string . 这里我们使用字符串的位置格式。 then we can see that x is at position 0 and y is at position 1. 那么我们可以看到x在位置0,y在位置1。

we can consider "{:.{}f}" it as " {value of x:{value of y}f} " 我们可以将"{:.{}f}"视为“ {value of x:{value of y}f}

now value of x is 100 and value of y is 0 so the string will look like "{100:.{0}f}". 现在x的值为100,y的值为0,因此字符串看起来像“ {100:。{0} f}”。 that means that there is 0 or no digits after decimal 这表示小数点后有0或没有数字

when print("{:.{}f}".format(x,y)) will execute it will print "100" 当执行print("{:.{}f}".format(x,y))时,它将打印“ 100”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM