Home  >  Article  >  Backend Development  >  Detailed explanation of round function usage

Detailed explanation of round function usage

DDD
DDDOriginal
2023-11-27 13:55:363076browse

The round function is used to round numbers. You need to pay attention to its rounding rules and the value range of the parameters.

Detailed explanation of round function usage

#The round function is a built-in function in Python that is used to round numbers. It is used as follows:

round(number, ndigits)

where number is the number to be rounded, and ndigits is the number of decimal places to be retained. The meaning and usage of these two parameters are explained in detail below.

number parameters:

number can be an integer, floating point number, fraction or complex number. No matter what type the number is, the round function will convert it to a floating point number for rounding.

ndigits parameter:

ndigits is an optional parameter that specifies the number of decimal places to retain. If this parameter is omitted, it defaults to 0, which means that integers are rounded. If ndigits is a positive number, it indicates the number of decimal places to keep; if ndigits is negative, it indicates the number of integer digits to round to.

The following are some specific examples to illustrate the use of the round function:

Example 1:

print(round(3.14159))  # 输出:3
print(round(3.14159, 2))  # 输出:3.14
print(round(3.14159, -1))  # 输出:0
print(round(3.14159, -2))  # 输出:0

In example 1, the first The function call rounds the integer 3, and since no decimal places are specified, the result is 3. The second function call rounds the decimal 3.14159 to two decimal places, resulting in 3.14. The third function call rounds the decimal 3.14159 to an integer number of digits, which results in 0. The fourth function call rounds the decimal 3.14159 to tens, which results in 0.

Example 2:

print(round(2.5))  # 输出:2
print(round(2.5, 0))  # 输出:2
print(round(2.5, 1))  # 输出:2.5

In example 2, the first function call rounds the decimal 2.5, resulting in 2. The second function call rounds the decimal 2.5 to an integer number of digits, resulting in 2. The third function call rounds the decimal number 2.5 to one decimal place, resulting in 2.5.

It should be noted that the rounding rule of the round function is based on "Round half to even" (Round half to even). This means that when the first digit of the decimal place to be rounded is 5, the rounding direction will be determined based on the parity of the previous digit. For example, the result of round(2.5) is 2, and the result of round(3.5) is 4.

In addition, it should be noted that the return value of the round function is a floating point number. If you need to get an integer, you can use the int function to convert it to an integer.

The round function is a built-in function in Python that is used to round numbers. It accepts two parameters: number represents the number to be rounded, and ndigits represents the number of decimal places to retain. The return value of the round function is a floating point number, which can be converted to an integer using the int function. When using the round function, you need to pay attention to its rounding rules and the value range of the parameters.

The above is the detailed content of Detailed explanation of round function usage. 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