# Convert float to hex in Python

In this tutorial, we will learn to convert a float into a hex value using Python Programming Language, before going into the topic let us understand how the hex() function works.

The hexadecimal system is a numeral representation system in which the value of the base is 16, in other words, the hexadecimal numeral system describes a base 16 number system. The hexadecimal numbers are denoted by 0-9 and A-F.

## Applying hex() method to convert float to hex in Python

The hex() function basically converts an integer number to the corresponding hexadecimal string representation. We can also find out the hexadecimal value of a negative integer.

hex(value)

The hex() function consists of only one parameter, the value should be an integer type number. It will convert the provided integer value to hexadecimal and returns a string.

#### Example:

hex(24)

Output: '0x18'

From the above code, we can see that the hex() function successfully converts the int 24 into a hexadecimal string ‘0x18’.

Let us now try inserting a float type value into the hex() function.

#### Example:

hex(24.5)

Output:TypeError: 'float' object cannot be interpreted as an integer

From the above code, we can see that the hex() function does not give us the hexadecimal string of a float type number, this is because the parameter takes in only integer type values.

## Using float.hex():

By using the above method we can find out the hexadecimal strings for a given float type value.

### Example:

float.hex(24.5)

Output: '0x1.8800000000000p+4'

From using the above method float.hex() we can finally convert a float type number into a hexadecimal string.

#### Conclusion:

Hence if the value is an integer we use ‘hex(value)’ and if it is a float we use ‘float.hex()’ to find out the hex value.

Also read:

## Leave a Reply