美文网首页
python3学习笔记:对数值做格式化输出

python3学习笔记:对数值做格式化输出

作者: 潼潼夏 | 来源:发表于2020-03-01 17:07 被阅读0次

对一个数值进行格式化输出,包括控制位数、对齐、包含千位分隔符以及其他一些细节;使用内建的format()函数即可。

x = 1123.4567788
print (format(x,'0.2f'))
#Right justified in 10 chars, one-digit accuracy
print (format(x, '>10.1f'))
#left justified
print (format(x, '<10.1f'))

#centered
print (format(x, '^10.1f'))

#inclusion of thousands separator
print (format(x, ','))
print (format(x, '0,.1f'))

运行结果:


image.png

如果需要采用科学计数法,只要把f改为e或者E即可:

format (x, 'e')
#1.123457e+03

相关文章

网友评论

      本文标题:python3学习笔记:对数值做格式化输出

      本文链接:https://www.haomeiwen.com/subject/cmgckhtx.html