python float hex()方法,转float为16进制

float hex()方法

python内置的float类方法hex(),可以将调用对象,即一个浮点数转换为十六进制值表示的字符串。float的hex()方法与python内置的hex()函数有所不同,后者可以将int类型转换为16进制字符串。相反,如果是int类型对象调用float的hex()方法,python会抛出AttributeError。


语法

floatObj.hex( self, / )

python源码中对float的hex()方法的介绍:

    Return a hexadecimal representation of a floating-point number.
    
    >>> (-0.1).hex()
    '-0x1.999999999999ap-4'
    >>> 3.14159.hex()
    '0x1.921f9f01b866ep+1'

参数

无。若传递参数,python会抛出TypeError。


返回值

float类型值的16进制字符串。


hex()方法实例代码

>>> (1.1).hex()
'0x1.199999999999ap+0'
>>> (1).hex() #int调用float的hex()方法
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'hex'

全栈后端 / python教程 :


























Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.