Python:如何使用new.format()输出小数点后两位数的浮点值?

Python:如何使用new.format()输出小数点后两位数的浮点值?,python,python-3.x,formatting,Python,Python 3.x,Formatting,我在自学Python以获得工作机会。我刚刚开始,似乎无法协调如何使用print“.format()输出小数点后只有两位数的浮点值 我试着把.2f放在不同的地方;不走运 这就是我所拥有的: def displayEmployeeData(self): print "Name: {:<10} ID: {:<10} Team: {:<10} salary: ${:<10} tenure: {:<10} months".format(self.name, self.

我在自学Python以获得工作机会。我刚刚开始,似乎无法协调如何使用print“.format()输出小数点后只有两位数的浮点值

我试着把.2f放在不同的地方;不走运

这就是我所拥有的:

def displayEmployeeData(self):
    print "Name: {:<10} ID: {:<10} Team: {:<10} salary: ${:<10} tenure: {:<10} months".format(self.name, self.ID, self.team, self.salary, self.tenure)
def displayEmployeeData(self):

print“Name:{:您需要使用
{0:.xf}
,其中0是参数编号,x是所需的位数

>>> print "{0:.3f} something".format(2.234234239)
>>> 2.234 something

有关更多格式设置的详细信息,请浏览。

您需要使用
{0:.xf}
,其中0是参数编号,x是所需的位数

>>> print "{0:.3f} something".format(2.234234239)
>>> 2.234 something
有关更多格式的详细信息,请参阅。

格式如下

print '{arg_num: format} string".format(<tuple of args>)
它的格式如下

print '{arg_num: format} string".format(<tuple of args>)

您有以下两种方法:

print("{0:.3f} something".format(2.234234239))
print("{0:<10.3f} something".format(2.234234239))

您有以下两种方法:

print("{0:.3f} something".format(2.234234239))
print("{0:<10.3f} something".format(2.234234239))

使用此
{.2f}
使用此
{.2f}
如果我已经有了{:如果我已经有了{:如果我已经有了{:你可以使用
{0:如果我已经有了{:你可以使用
{0:我已经有了对答案的编辑。我已经有了对答案的编辑:查看。