python----小数点精度控制round()
2015-10-21 14:24
501 查看
python版本也会影响结果,python2把x四舍五入为远离0的最近倍数,如round(0.5)=1, round(-0.5)=-1;
python3则会把x四舍五入为最近的偶数倍数,如round(0.5)=0, round(1.5)=2.0, round(2.5)=2.0,下面都是python2.7的输出结果
1.直接输出
整除:整除得整数,整除有小数点得一位小数
结果:
3
3.0
3.0
3.0
3.0
3.0
3.0
非整除:得真实结果
2.精度控制
高精度---低精度 round()
round()-不指定,取整,四舍五入(如果遇到.5偶弃奇进)
round()--指定小数点位数,四舍五入(如果遇到.5相反,偶进一位奇舍弃)
格式化
python3则会把x四舍五入为最近的偶数倍数,如round(0.5)=0, round(1.5)=2.0, round(2.5)=2.0,下面都是python2.7的输出结果
1.直接输出
整除:整除得整数,整除有小数点得一位小数
print 6/2 print 6/2.0 print 6/2.00 print 6/2.000 print 6.0/2 print 6.00/2 print 6.000/2
结果:
3
3.0
3.0
3.0
3.0
3.0
3.0
非整除:得真实结果
print 6.1/2 print 6.10/2 print 6.100/2 print 6.11/2
2.精度控制
高精度---低精度 round()
round()-不指定,取整,四舍五入(如果遇到.5偶弃奇进)
print round(2.3) print round(2.6) print round(2.5) print round(1.5)
round()--指定小数点位数,四舍五入(如果遇到.5相反,偶进一位奇舍弃)
print round(2.635, 2) print round(2.645, 2) print round(2.655, 2) print round(2.665, 2) print round(2.675, 2)
格式化
print ("%.2f" % 2.635) print int(2.635)
相关文章推荐
- python JSON性能测试与simplejson对比
- python 模块安装
- Python爬虫:动态网页抓取淘宝“淘女郎”照片
- what yield does in python?
- Python面向对象详解
- Python notes
- Python 正则表达式(分组)
- 关于python的执行顺序和@操作符
- Python 正则表达式(常用函数)
- Python 正则表达式(基础)
- python之时间处理模块 -- time
- Build a Python IDE for Windows with Notepad++ and IPython
- python GUI 实例
- Python学习笔记2
- python __init__.py
- Python笔记:OptionParse模块的使用
- usr/bin/env python 和 usr/bin/python
- 关于用mapreduce做kmeans聚类以及python的numpy和list做矩阵、向量乘法的速度对比
- Python的metaclass、`__new()__`、单例模式
- python脚本——合并word