python 数值处理内置函数

int(obj)  转换成整型

>>> 10 + 1
11
>>> 10 + int("30")
40
>>> 10 + int("1101")
1111
>>> 10 + int("1101", 2)
23
>>> 10 + int("1101", 8)
587
>>> int("1101", 16)
4353
>>> 
float(obj) 转化成浮点型

>>> float("3.1") * 10
31.0
bin(obj) 转换成2进制

>>> bin(15)
'0b1111'
hex(obj) 转换成16进制

>>> hex(11)
'0xb'
oct(obj)转化成8进制

>>> oct(10)
'0o12'


math

>>> import math

.floor(obj) 总是取小的

>>> math.floor(22/7)
3
>>> math.floor(3.99)
3
>>> math.floor(-3.14)
-4
.trunc(obj) 总是趋向于0

>>> import math
>>> math.trunc(3.14)
3
>>> math.trunc(3.99)
3
>>> math.trunc(-3.14)
-3
.ceil(obj) 总是趋向于大

>>> math.ceil(3.14)
4
>>> math.ceil(-3.14)
-3

.pi圆周率

>>> math.pi
3.141592653589793


round(obj) 四舍五入

>>> round(3.14)
3
>>> round(3.58)
4
>>> round(3.5)
4
pow(a,b) a的b次方

>>> pow(2,3)
8
sqrt(obj) 开平方

>>> math.sqrt(12)
3.4641016151377544
>>> math.floor(math.sqrt(12))
3


decimal

>>> 0.1 + 0.1 + 0.1 - 0.3
5.551115123125783e-17
>>> import decimal
>>> x = decimal.Decimal('0.1')
>>> y = decimal.Decimal('0.3')
>>> x + x + x - y
Decimal('0.0')
>>> from decimal import Decimal
>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值