Python round() Function: Round Numbers | Syntax, Examples and Use Cases

Introduction: Python round() Function

When working with numbers in Python, there are situations where values contain more decimal places than required. Whether you’re displaying prices, calculating averages, or presenting measurement results, it is often necessary to round numbers to make them easier to read and use.

Without a built-in rounding function, you would need to write additional logic to adjust decimal values manually, making programs more complex and difficult to maintain.

This is where the Python round() Function helps simplify rounding numerical values.

What it is: The round() function is a built-in Python function that rounds a number to the nearest integer or to a specified number of decimal places. The number of decimal places is controlled using an optional parameter.

The Python round() Function provides a simple and reliable way to round numbers for calculations, reports, and user-friendly output.

Take a look at a quick example to see how it works.

You can also explore its practical use cases to understand where it is used.

Now let’s understand its syntax, parameters, and return value before exploring practical examples.

💡 Tip: Interested in exploring more Python built-in functions? Continue with the Built-in Functions Learning Guide to learn each function through clear explanations and practical examples.

Syntax, Parameters, Return Value and Examples: Python round() Function

The following section explains the syntax, parameters, return value, and a quick example of the Python round() Function.

Syntax

round(number, ndigits)

Parameters

Parameter Description
number The numeric value to be rounded.
ndigits (optional) Specifies the number of decimal places to round the value to. If omitted or None, the number is rounded to the nearest integer.

Return Value

Return Value Description
int | float Returns the rounded value. The return type depends on the input value and whether ndigits is specified.

Quick Example

The following example rounds a floating-point number to two decimal places using the round() function.

price = 149.9876

print(round(price, 2))


# Output:
149.99

The round() function rounds the number to the specified number of decimal places and returns the rounded value.

How the Python round() function works

  • The round() function accepts a numeric value as its first argument.
  • The optional ndigits parameter specifies the number of decimal places.
  • If ndigits is omitted, the value is rounded to the nearest integer.
  • It works with integers and floating-point numbers.
  • The function returns a new rounded value without modifying the original value.
  • The returned value can be used directly in calculations or displayed as output.

Examples: Python round() Function

The following examples show how the Python round() Function works in different programming scenarios.

Example 1: Rounding a Floating-Point Number

number = 8.76

print(round(number))


# Output:
9

Explanation: Since no second argument is provided, the round() function rounds the value to the nearest integer.

Example 2: Rounding to Two Decimal Places

price = 149.9876

print(round(price, 2))


# Output:
149.99

Explanation: The second argument specifies that the value should be rounded to two decimal places.

Example 3: Rounding to the Nearest Ten

number = 347

print(round(number, -1))


# Output:
350

Explanation: A negative value for ndigits rounds the number to the nearest ten, hundred, thousand, and so on.

Example 4: Rounding User Input

number = float(input("Enter a decimal number: "))

print(round(number, 1))


# Sample Output:
Enter a decimal number: 15.67
15.7

Explanation: The user input is converted to a floating-point number before being rounded to one decimal place.

Example 5: Using round() in Calculations

average = 

average = (82.45 + 91.82 + 88.67) / 3

print(round(average, 2))


# Output:
87.65

Explanation: The round() function is commonly used to present calculation results with the required level of precision.

Example 6: Understanding Python’s Rounding Rule

print(round(2.5))
print(round(3.5))


# Output:
2
4

Explanation: When a value ends in .5, Python rounds it to the nearest even integer. This behavior, known as banker’s rounding, helps reduce rounding bias in repeated calculations.

Use Cases: When to use the round() Function

Below are some common situations where the Python round() Function becomes useful:

  • Rounding decimal numbers for display.
  • Formatting calculation results to a specific number of decimal places.
  • Working with financial and accounting values.
  • Presenting measurements and scientific data.
  • Processing user input that requires rounded values.
  • Reducing unnecessary decimal places in reports and calculations.

Key Takeaways: round() Function

Before wrapping up, here are the key points to remember about the Python round() Function:

  • The round() function returns the rounded value of a number.
  • The optional ndigits parameter specifies the number of decimal places.
  • If ndigits is omitted, the value is rounded to the nearest integer.
  • Negative values of ndigits round to tens, hundreds, thousands, and so on.
  • The original value is not modified.
  • Python uses banker’s rounding for values ending in .5.

In short, the Python round() Function provides a simple and reliable way to round numeric values, making calculations and output easier to read and present.

Leave a Comment

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

Scroll to Top