今天我们来看看python 中float 方法
1: 获取值的最简化结果
def as_integer_ratio(self): # real signatureunknown; restored from __doc__
"""
float.as_integer_ratio() -> (int,int)
Return a pair of integers, whoseratio is exactly equal to the original
float and with a positivedenominator.
Raise OverflowError on infinities anda ValueError on NaNs.
>>>(10.0).as_integer_ratio() #### 这里给出了使用方法,返回了元组类型
(10, 1)
>>> (0.0).as_integer_ratio()
(0, 1)
>>>(-.25).as_integer_ratio()
(-1, 4)
"""
2:复数取共轭
a = 1.1 b = 10.5 c = 2 + 1.3J ### 这个例子才能体现出来 print("a",a.conjugate()) print("b",b.conjugate()) print("c",c.conjugate()) a 1.1 b 10.5 c (2-1.3j)
3:十六进制转为浮点数
def fromhex(self, string): # real signature unknown; restored from __doc__ """ float.fromhex(string) -> float Create a floating-point number from a hexadecimal string. >>> float.fromhex('0x1.ffffp10') ### 使用方法 2047.984375 >>> float.fromhex('-0x1p-1074') -5e-324 """ return 0.0
4:将浮点数转换为十六进制
def hex(self): # real signature unknown; restored from __doc__ """ float.hex() -> string Return a hexadecimal representation of a floating-point number. >>> (-0.1).hex() #### 使用方法 '-0x1.999999999999ap-4' >>> 3.14159.hex() '0x1.921f9f01b866ep+1' """ return ""
5:判断是不是整数,返回bool 值
def is_integer(self, *args, **kwargs): # real signature unknown """ Return True if the float is an integer. """ pass
测试:
a = 1.1 b = 10.5 c = 2 + 1.3J d = 5.0 print("a",a.is_integer()) print("b",b.is_integer()) print("d",d.is_integer()) ######## 返回 结果 bool类型 a False b False d True