Ben*_*ire 1 python floating-point numbers python-3.x
我有一个不断变化的变量,它是一个浮点(例如:2.003)
我想自动将其更改为只有2位小数.
enr*_*cis 11
你可以使用圆形功能:
print round(2.003, 2) # 2.0
print round(2.013, 2) # 2.01
Run Code Online (Sandbox Code Playgroud)
否则保持精度并仅显示所需的小数位数:
print '%.2f' % 2.003 # 2.00
print '%.2f' % 2.013 # 2.01
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
129 次 |
| 最近记录: |