python float类型使用方法

今天我们来看看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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值