在Python中显示带有两个小数位的浮点数

时间:2021-12-03 17:08:22

I have a function taking float arguments (generally integers or decimals with one significant digit), and I need to output the values in a string with two decimal places (5 -> 5.00, 5.5 -> 5.50, etc). How can I do this in Python?

我有一个函数采用浮点数参数(通常是带有一个有效数字的整数或小数),我需要输出一个带有两个小数位的字符串中的值(5 - > 5.00,5.5 - > 5.50等)。我怎么能用Python做到这一点?

6 个解决方案

#1


101  

You could use the string formatting operator for that:

您可以使用字符串格式化运算符:

>>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'

The result of the operator is a string, so you can store it in a variable, print etc.

运算符的结果是一个字符串,因此您可以将其存储在变量,打印等中。

#2


150  

Since this post might be here for a while, lets also point out python 3 syntax:

由于这篇文章可能会在这里发表一段时间,让我们也指出python 3语法:

"{:.2f}".format(5)

#3


6  

f-string formatting:

f字符串格式:

This is new in Python 3.6 - the string is placed in quotation marks as usual, prepended with f'... in the same way you would r'... for a raw string. Then you place whatever you want to put within your string, variables, numbers, inside braces f'some string text with a {variable} or {number} within that text' - and Python evaluates as with previous string formatting methods, except that this method is much more readable.

这是Python 3.6中的新功能 - 字符串像往常一样放在引号中,前缀为f'...就像你对原始字符串一样。然后你把你想要放在你的字符串,变量,数字,内部大括号fofome字符串文本中的任何内容放在那个文本中的{variable}或{number} - 并且Python的评估与之前的字符串格式化方法一样,除了这个方法更具可读性。

>>>a = 3.141592
>>>print(f'My number is {a:.2f} - look at the nice rounding!')

My number is 3.14 - look at the nice rounding!

You can see in this example we format with decimal places in similar fashion to previous string formatting methods.

您可以在此示例中看到,我们使用与先前字符串格式化方法类似的方式格式化小数位。

NB a can be an number, variable, or even an expression eg f'{3*my_func(3.14)}:02f}'.

NB a可以是数字,变量,甚至是表达式,例如f'{3 * my_func(3.14)}:02f}'。

#4


5  

String formatting:

字符串格式:

print "%.2f" % 5

#5


4  

Using python string formatting.

使用python字符串格式。

>>> "%0.2f" % 3
'3.00'

#6


1  

Using Python 3 syntax:

使用Python 3语法:

print('%.2f' % number)

#1


101  

You could use the string formatting operator for that:

您可以使用字符串格式化运算符:

>>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'

The result of the operator is a string, so you can store it in a variable, print etc.

运算符的结果是一个字符串,因此您可以将其存储在变量,打印等中。

#2


150  

Since this post might be here for a while, lets also point out python 3 syntax:

由于这篇文章可能会在这里发表一段时间,让我们也指出python 3语法:

"{:.2f}".format(5)

#3


6  

f-string formatting:

f字符串格式:

This is new in Python 3.6 - the string is placed in quotation marks as usual, prepended with f'... in the same way you would r'... for a raw string. Then you place whatever you want to put within your string, variables, numbers, inside braces f'some string text with a {variable} or {number} within that text' - and Python evaluates as with previous string formatting methods, except that this method is much more readable.

这是Python 3.6中的新功能 - 字符串像往常一样放在引号中,前缀为f'...就像你对原始字符串一样。然后你把你想要放在你的字符串,变量,数字,内部大括号fofome字符串文本中的任何内容放在那个文本中的{variable}或{number} - 并且Python的评估与之前的字符串格式化方法一样,除了这个方法更具可读性。

>>>a = 3.141592
>>>print(f'My number is {a:.2f} - look at the nice rounding!')

My number is 3.14 - look at the nice rounding!

You can see in this example we format with decimal places in similar fashion to previous string formatting methods.

您可以在此示例中看到,我们使用与先前字符串格式化方法类似的方式格式化小数位。

NB a can be an number, variable, or even an expression eg f'{3*my_func(3.14)}:02f}'.

NB a可以是数字,变量,甚至是表达式,例如f'{3 * my_func(3.14)}:02f}'。

#4


5  

String formatting:

字符串格式:

print "%.2f" % 5

#5


4  

Using python string formatting.

使用python字符串格式。

>>> "%0.2f" % 3
'3.00'

#6


1  

Using Python 3 syntax:

使用Python 3语法:

print('%.2f' % number)