Home >Backend Development >Python Tutorial >How to convert python binary to decimal?

How to convert python binary to decimal?

青灯夜游
青灯夜游Original
2019-06-05 10:14:1757713browse

How to convert python binary to decimal?

In python, you can use the built-in function int() function to convert binary to decimal; the int() function can convert a numeric string or decimal number in a specified base into an integer. . (Recommended learning: python video tutorial)

Syntax:

int(object,base)

How to convert python binary to decimal?

Return value: Return integer data.

Note: When converting floating point numbers to integer data, no matter what the decimal part of the floating point number is, the int() function will only retain the integer part and round off the decimal part. go. Therefore, when solving problems such as rounding of floating point numbers, you should avoid using the int function directly.

Example 1: Convert a binary number to a decimal number

test = ['111011011111', '0b101']
 
for number in test:
    print(int(number, 2))

Output:

3807
5

Example 2: Convert an octal number Convert to decimal number

test = ['-1537202', '0o147']
 
for number in test:
    print(int(number, 8))

Output:

-441986
103

For more python related technical knowledge, please visit the python introductory tutorial column to learn!

The above is the detailed content of How to convert python binary to decimal?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn