Python round() Function – Tutorial with Examples

The round() function in Python is a built-in function that returns a floating-point number rounded to the specified number of decimal places. The round() function rounds a floating-point number to the nearest integer, or to the nearest multiple of 10 to the power minus n, where n is the number of decimal places to round to.

Syntax

round(number, ndigits)

Parameters

  • number : The number to be rounded.
  • ndigits : The number of decimal places to round the number to. Default value is 0. If ndigits is negative, the number will be rounded to the nearest 10^ndigits.

Return Value

The round() function returns a rounded floating-point number.

Examples

Example 1: Rounding a Floating-Point Number to the Nearest Integer

# Rounding a floating-point number to the nearest integer
x = 3.14159
print(round(x))

Output

3

In this example, a floating-point number is rounded to the nearest integer using the round() function. The resulting rounded integer is then printed to the console.

Example 2: Rounding a Floating-Point Number to a Specified Number of Decimal Places

# Rounding a floating-point number to 2 decimal places
x = 3.14159
print(round(x, 2))

Output

3.14

In this example, a floating-point number is rounded to 2 decimal places using the round() function. The resulting rounded floating-point number is then printed to the console.

Example 3: Rounding a Floating-Point Number to the Nearest 10^ndigits

# Rounding a floating-point number to the nearest 10^-2
x = 3.14159
print(round(x, -2))

Output

0.0

In this example, a floating-point number is rounded to the nearest 10^-2 using the round() function. The resulting rounded floating-point number is then printed to the console.

Use Cases

The round() function is used in various applications, including financial and mathematical computations, where precision is important. For example, in financial applications, it is often necessary to round monetary values to a specified number of decimal places, and the round() function can be used for this purpose. The function can also be used to round numbers for display purposes, for example, to round PI to a certain number of decimal places for display purposes in a scientific or mathematical application.

Another use case for the round() function is in machine learning and data analysis, where it is often necessary to round the results of computations for improved interpretability and readability. In these applications, the round() function can be used to ensure that results are displayed in a way that is clear and easy to understand.

Additionally, the round() function can be used in control structures, such as conditional statements and loops, to control the flow of the program based on rounded values. For example, if the result of a computation is rounded to a certain value, a certain branch of code can be executed, or a certain loop can be repeated a specific number of times.

Overall, the round() function is a simple but powerful tool in the Python language, providing a flexible and easy-to-use way to round floating-point numbers to a specified number of decimal places or to the nearest multiple of 10^ndigits.

Leave a Reply

Your email address will not be published. Required fields are marked *