Home > Backend Development > Python Tutorial > Introduction to Python functions: usage and examples of round function

Introduction to Python functions: usage and examples of round function

WBOY
Release: 2023-11-03 10:03:23
Original
4806 people have browsed it

Introduction to Python functions: usage and examples of round function

Introduction to Python functions: Usage and examples of round function

Overview:
In Python, round() is a built-in function used to perform operations on numbers. rounding. It can round floating point or integer values ​​to a specified number of decimal places. The round() function has the following syntax:
round(number, ndigits)
where number is the number to be rounded, and ndigits is the number of decimal places to be retained.

Usage examples:

  1. Rounding to integers
    If the ndigits parameter is 0 or omitted, a floating point number or integer can be rounded to the nearest integer value.
num1 = 10.4
num2 = 10.6

print(round(num1))  # 输出:10
print(round(num2))  # 输出:11
Copy after login
  1. Round to the specified number of decimal places
    If the ndigits parameter is a positive integer, a floating point number or integer can be rounded to the specified number of decimal places.
num = 3.1415926

print(round(num, 2))  # 输出:3.14
print(round(num, 4))  # 输出:3.1416
Copy after login
  1. Rounding to negative digits
    If the ndigits parameter is a negative integer, a floating point number or integer can be rounded to the specified number of digits, that is, the number of digits to the left of the decimal point.
num = 9876

print(round(num, -1))  # 输出:9900
print(round(num, -2))  # 输出:9800
Copy after login

Some notes:

  1. The round() function returns a floating point number, even if the integer is passed in.
  2. When the number to be rounded is equidistant from two integers, the round() function returns the nearest even integer.
num1 = 2.5
num2 = 3.5

print(round(num1))  # 输出:2
print(round(num2))  # 输出:4
Copy after login

Conclusion: The
round() function is a very useful function that can be used to round numbers. It can be rounded to an integer or to a specified number of decimal places. In some cases, it can also be used to round a number to a specified number of negative digits. Whether you are dealing with financial data, scientific calculations, or other scenarios that require precise processing of numbers, the round() function can come in handy.

The above is the detailed content of Introduction to Python functions: usage and examples of round function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template