sec01 - Conversion between integers and floating-point numbers

Not limited to Python, programming languages can convert types. You can convert between int and float types, or convert numbers to strings (a sequence of characters representing numbers). In this lecture, we will learn about conversions involving numbers.

(Converting numbers to characters and characters to numbers will be covered after you have completed learning about the String type.)

Conversion between integers and floating-point numbers

Python handles calculation results flexibly, returning values as floats when they include decimal points. However, there may be cases where you want the result to be an int rather than a float. In this lecture, we will explain how to write code for such cases.

int()

int()” is a function that converts the number entered in parentheses to an int type (integer). “int()” behaves like “removing the decimal part,” so “4.8” becomes “4” and “-4.8” becomes “-4.” Note that this behavior is not like ‘rounding’ or “rounding toward negative infinity.”

Write some code and check the results.

# (A)
result1 = int(4.8)
print('(A) int(4.8):', result1)

(A) First, let's look at the basic form. Enter a number in parentheses, such as “int(4.8),” and execute it. The value entered in the parentheses can be either a floating-point number or an integer, and it will function without any issues. (However, in the case of integers, the same value will be output as the result.)

When you run the above code, “4” is output as the result. You can confirm that it behaves as if “the decimal part is removed.”

# (B)
float_number = -2.625
result2 = int(float_number)
print('(B) int(-2.625):', result2)

(B) Next, let's try executing the above code. This code assigns a negative floating-point number to a variable and then converts it to an integer using “int().” When writing code, you will often specify the variable inside parentheses.

When the above code is executed, the output is “-2.” Even though the value is negative, the operation is to “remove the decimal part,” so the “.625” part is deleted, and only “-2” remains in the output. Note: If the operation is “round to the nearest integer” or “round toward negative infinity,” the result will be “-3,” so please note that the behavior differs from these cases.

# (C)
numA = 8
numB = 5
result3 = int(numA / numB)
print('(C) int(8 / 5):', result3)

(C) The above code attempts to convert the result of division into an integer. The result of “8 / 5” is “1.6,” so the “.6” part is deleted and “1” is returned as the result.

# (D)
numA = -8
numB = 5
calc_result = numA / numB
result4 = int(calc_result)
print('(D) -8 / 5 :', calc_result)
print('(D) int(calc_result):', result4)

(D) This example is similar to (C), but first assigns the calculation result to the variable “calc_result” and then converts it to an integer. In this way, you can split the ‘calculation’ and “conversion to integer” into two separate lines.

The result of the code in (D) is that the calculation of “-8 / 5” yields “-1.6,” which is then converted to an integer using “int().” The decimal part of “-1.6” is removed, resulting in “-1” as the final output.

float()

“float()” is a function that converts the value specified in parentheses to a float type (floating-point number).

Try executing the following code.

# (E)
result5 = float(5)
print('(E) float(5):', result5)

This code converts the integer “5” to a floating-point number using “float()”. The result is “5.0”. Even when converting an integer to a float, only “.0” is added, and the value remains the same. Similarly, if you enter a floating-point number inside parentheses, the same value is output.

Additionally, in Python 3, when the result of an integer division is a floating-point number, it is returned as a float type. Therefore, I believe that the process of converting numbers to floats is rarely used.

When using “float()” in Python 3, you may need to convert strings to floating-point numbers. For example, the string‘123.4’” cannot be used in arithmetic operations such as addition or subtraction, so it must be converted to the number123.4.” (The conversion between strings and numbers will be explained in a separate lecture.)

Converting float() in languages other than Python3

As explained in previous lectures, in Python 3, even when dividing “integer/integer,” if the calculation result is a floating-point number, the result is returned as a float type.

However, Python 2 and other programming languages return “integer/integer” results as integers only. Therefore, it is necessary to incorporate floating-point numbers into calculations as appropriate.

result = float(numA) / numB

As shown above, if the value assigned to a variable may be an integer, use “float()” to ensure that floating-point numbers are included in the calculation formula.

result = numA / 5.0

Also, if the divisor or dividend is a constant, explicitly include the floating-point number in the calculation expression, such as “5.0,” to obtain a floating-point result.

(※ For Python3, the calculation results are automatically returned as integers or floating-point numbers, so this kind of handling isn't needed.)

おすすめの記事