Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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/1/ms-access/4.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中的精度_Python - Fatal编程技术网

python中的精度

python中的精度,python,Python,如何用python编写一个打印语句,在小数点后正好打印2位数字 x = 4.12121212 print '%.2f' % x 基本上,与在C中使用printf的方法相同 print "{0:.2f}".format(your_number) 这将在中详细解释 还有一个用于定点小数的特殊模块。更多信息:这是django浮点格式模板标记,您可能会感兴趣阅读 第92行: 另一种有效的方法是: import sys a = 0.5 sys.stdout.write(str('%0.2f'%a))

如何用python编写一个打印语句,在小数点后正好打印2位数字

x = 4.12121212
print '%.2f' % x
基本上,与在C中使用printf的方法相同

print "{0:.2f}".format(your_number)
这将在中详细解释


还有一个用于定点小数的特殊模块。更多信息:

这是django浮点格式模板标记,您可能会感兴趣阅读

第92行:


另一种有效的方法是:

import sys
a = 0.5
sys.stdout.write(str('%0.2f'%a))

@Philando:从Python3.0开始,我建议的格式应该优先于此格式。如果你想让你的代码成为未来的证明,你应该记住..@Space_C0wb0:同意并感谢更新:-),但考虑到我正在处理的问题,不太需要它。
import sys
a = 0.5
sys.stdout.write(str('%0.2f'%a))