Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当四舍五入到最接近的百分之时,如何包含0_Python_Python 3.x_Numbers_Rounding - Fatal编程技术网

Python 当四舍五入到最接近的百分之时,如何包含0

Python 当四舍五入到最接近的百分之时,如何包含0,python,python-3.x,numbers,rounding,Python,Python 3.x,Numbers,Rounding,假设我有以下代码: num = 1.29283 round(num, 2) 这四舍五入到1.29,但如果我这样做: num = 1.30293 round(num, 2) 这四舍五入到1.3。我想知道有没有办法把它调到1点30分;我知道它是相同的数字,但我需要它来打印1.30。您可以使用字符串格式。python中的数字没有尾随的零。所以你的问题只对字符串有意义 例如: >>> num = 1.30293 >>> "{:.2f}".format(num) '

假设我有以下代码:

num = 1.29283
round(num, 2)
这四舍五入到1.29,但如果我这样做:

num = 1.30293
round(num, 2)

这四舍五入到1.3。我想知道有没有办法把它调到1点30分;我知道它是相同的数字,但我需要它来打印1.30。

您可以使用字符串格式。python中的数字没有尾随的零。所以你的问题只对字符串有意义

例如:

>>> num = 1.30293
>>> "{:.2f}".format(num)
'1.30'

.2f
表示这是一个浮点(
f
),您需要在点
.2
后加两位数字。阅读有关字符串格式的更多信息

1.3
1.30
是相同的数字。如果您关心输出的表示形式,那么您需要的是字符串,而不是数字:查看字符串格式。非常感谢!