python中取整的几种方法

#encoding:utf-8
import math

#向上取整
print "math.ceil---"
print "math.ceil(2.3) => ", math.ceil(2.3)
print "math.ceil(2.6) => ", math.ceil(2.6)

#向下取整
print "\nmath.floor---"
print "math.floor(2.3) => ", math.floor(2.3)
print "math.floor(2.6) => ", math.floor(2.6)

#四舍五入
print "\nround---"
print "round(2.3) => ", round(2.3)
print "round(2.6) => ", round(2.6)

#这三个的返回结果都是浮点型
print "\n\nNOTE:every result is type of float"
print "math.ceil(2) => ", math.ceil(2)
print "math.floor(2) => ", math.floor(2)
print "round(2) => ", round(2)

返回如下:

原文地址:https://www.cnblogs.com/VseYoung/p/python_numbers.html

时间: 2024-08-01 23:40:25

python中取整的几种方法的相关文章

介绍PHP取整的四种方法

PHP实现取整的问题,不仅在我们学习PHP过程中会遇到,在我们PHP面试过程中也是常见的考点之一. 下面我们结合简单的示例给大家总结介绍PHP取整的四种方法. 第一种方法:直接取整,舍弃小数,保留整数 1 2 3 4 5 6 7 <?php $num = 3.1415926; $num2 = 3.6; echo intval($num).'<br>'; echo  intval($num2); echo "<hr>"; ?> 结果如下: 3 3 第二

Python爬取网页的三种方法

# Python爬取网页的三种方法之一:  使用urllib或者urllib2模块的getparam方法 import urllib fopen1 = urllib.urlopen('http://www.baidu.com').info() fopen2 = urllib2.urlopen('http://www.sina.com').info() print fopen1.getparam('charset') print fopen2.getparam('charset') #----有些

python中执行shell的两种方法总结

这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包均是Python现有的内置模块.需要的朋友可以参考借鉴,下面来一起看看吧. 一.使用python内置commands模块执行shell commands对Python的os.popen()进行了封装,使用SHELL命令字符串作为其参数,返回命令的结果数据以及命令执行的状态: 该命令目前已经废弃,被s

javaScript中小数取整,四种方法的比较

1.parseInt:只取整数位例如:parseInt(3.7) 取整结果为:3parseInt(-1.1) 取整结果为:-1 2.Math.floor :向下去整,取大的整数例如:Math.floor(3.7) 取整结果为:4Math.floor(-1.1) 取整结果为:-1 3.Math.ceil :向上去整,取小的整数例如:Math.floor(3.7) 取整结果为:3Math.floor(-1.1) 取整结果为:-2 4.Math.round:四舍五入例如:Math.round(3.3)

JS取整的四种方法

parseInt() 该方法取整是把小数点后面小数去掉,只保留整数部分.如果要取整的数为正时,类似Math.floor();为负时,类似Math.ceil() ? Math.round() 四舍五入取整 ? Math.floor() 向下取整? 如Math.floor(1.8) 返回 1:Math.floor(-1.8) 返回 -2; ? Math.ceil()向上取整 ?如Math.ceil(1.8)返回 2:Math.ceil(-1.8) 返回 -1; 原文地址:https://blog.5

python中字符串格式化的四种方法

1 name = "huangemiling" 2 age= 10 3 address = 'nanjing' 4 print("My name is %s,age is %d,I come from %s"%(name,age,address)) 5 print("My name is {0},age is {1},I come from {2}".format(name,age,address)) 6 print("My name

[Python]从url中解析域名的几种方法

Python从url中解析域名的几种方法 从url中找到域名,首先想到的是用正则,然后寻找相应的类库.用正则解析有很多不完备的地方,url中有域名,域名后缀一直在不断增加等.通过google查到几种方法,一种是用Python中自带的模块和正则相结合来解析域名,另一种是使第三方用写好的解析模块直接解析出域名. 要解析的url urls = ["http://meiwen.me/src/index.html", "http://1000chi.com/game/index.htm

Python抓取网页&amp;批量下载文件方法初探(正则表达式+BeautifulSoup) (转)

Python抓取网页&批量下载文件方法初探(正则表达式+BeautifulSoup) 最近两周都在学习Python抓取网页方法,任务是批量下载网站上的文件.对于一个刚刚入门python的人来说,在很多细节上都有需要注意的地方,以下就分享一下我在初学python过程中遇到的问题及解决方法. 一.用Python抓取网页 基本方法: [python] view plaincopyprint? import urllib2,urllib url = 'http://www.baidu.com' req 

用Python进行时间序列预测的7种方法

数据准备 数据集(JetRail高铁的乘客数量)下载. 假设要解决一个时序问题:根据过往两年的数据(2012 年 8 月至 2014 年 8月),需要用这些数据预测接下来 7 个月的乘客数量. import pandas as pd import numpy as np import matplotlib.pyplot as plt df = pd.read_csv('train.csv') df.head() df.shape 依照上面的代码,我们获得了 2012-2014 年两年每个小时的乘