类型转换和舍入
类型转换
转化为int整型
int(<var>)
a = 3.1415926
print(int(a))
# 3
转化为float浮点型
float(<var>)
,最多保留16位有效数字
例子1
a = 6
b = float(a)
print(b)
# 6.0
例子2
a = 6.12345
b = float(a)
print(b)
# 6.12345
例子3
a = 6.123456789012345
b = float(a)
print(b)
# 6.123456789012345
例子4
a = 6.1234567890123456789
b = float(a)
print(b)
# 6.123456789012345
# 超出精度的部分被社区,仅保留16位有效数字
*转化为complex复数型
complex(<var>, <var>
例子1
a = complex(2)
b = complex(0,3)
c = complex(2,3)
print(a)
print(b)
print(c)
# (2+0j)
# 3j
# (2+3j)
例子2
a = complex(2)
b = complex(0,3)
c = complex(2,3)
if a+b == c:
print('a+b和c等价')
# a+b和c等价
舍入(四舍五入)
round(<var>, <precision>)
<var>
被四舍五入的值,<percision>
小数点后保留的位数
例子1(四舍)
a = 6.123
b = round(a, 2)
print(b)
# 6.12
例子2(五入)
a = 6.11171
b = round(a, 3)
print(b)
# 6.112