Home  >  Article  >  Backend Development  >  How to use the round() function in Python to round floating point numbers

How to use the round() function in Python to round floating point numbers

WBOY
WBOYOriginal
2023-08-22 10:15:442806browse

How to use the round() function in Python to round floating point numbers

How to use the round() function to round floating-point numbers in Python

In Python, rounding floating-point numbers is a common requirement. Python provides the built-in round() function to implement this function. Floating point numbers can be rounded easily by using the round() function.

The use of the round() function is very simple. Its syntax is as follows:

round(number, ndigits)

Among them, number is the floating point number to be rounded, and ndigits is the number of decimal places retained. ndigits is an optional parameter and defaults to 0 when omitted, indicating that the integer part is rounded.

The following are several specific examples to illustrate the use of the round() function.

Example 1: Rounding a floating point number

num = 3.14159
rounded_num = round(num)
print(rounded_num)  # 输出 3

In this example, we round the floating point number 3.14159. Since the ndigits parameter is omitted, the integer part is rounded by default, so The output is 3.

Example 2: Specify the number of decimal places to retain for rounding

num = 3.14159
rounded_num = round(num, 2)
print(rounded_num)  # 输出 3.14

In this example, we round the floating point number 3.14159 and specify the number of decimal places to retain. The output is 3.14.

Example 3: Rounding negative numbers

num = -3.5
rounded_num = round(num)
print(rounded_num)  # 输出 -4

In this example, we round the negative number -3.5, and the integer part is rounded by default. The output is -4.

It should be noted that the rounding rule of the round() function is based on the nearest even number. When the last decimal digit is exactly 5, if the previous digit is an even number, it is rounded down; if the previous digit is an odd number, it is rounded up. This rule ensures fairer rounding results.

In addition to basic rounding, the round() function also has some special use cases. When ndigits is a negative number, it means that the part to the left of the decimal point is rounded. This is useful when dealing with special needs like scientific notation.

To summarize, the round() function in Python can easily round floating point numbers. By setting the ndigits parameter appropriately, you can flexibly control the number of decimal places retained. For number processing needs, the round() function is a practical tool.

The above is the detailed content of How to use the round() function in Python to round floating point numbers. 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