• Python

Command Palette

Search for a command to run...

Native Functions
  • Python Abs Function
  • Python Any All Functions
  • Python Zip Function
  • Python Sum Function
  • Python Filter Function
  • Python Max Min Functions
  • Python Map Function
  • Python Round Function
  • Python Sorted Function
Data Types
  • Python Integers
  • Python Floats
  • Python Complex Numbers
  • Python List Type
  • Python Tuple Type
  • Python String Type
  • Python Range Type
Collections.abc Module
  • Python Containers
  • Python Hashable
  • Python Iterable
  • Python Iterators
  • Python Sequence
  • Python Mutable Sequence

Create an Account

FREE

Join our community to access more courses.

Create Account

On this page

What does round() do?Basic SyntaxExamples with integers and floatsUsing the ndigits parameterBehavior with .5Summary
      • Blog

      Python Round Function

      What does round() do?

      The round() function returns a number rounded to a given number of digits. By default, it rounds to the nearest integer.

      Basic Syntax

      round(number[, ndigits])
      
      • number: The number to round.
      • ndigits: (Optional) The number of decimal places to round to. If omitted, rounds to the nearest integer.

      Examples with integers and floats

      print(round(3.14159))      # Output: 3
      print(round(3.14159, 2))   # Output: 3.14
      print(round(123.456, -1))  # Output: 120.0 (rounds to nearest 10)
      

      Using the ndigits parameter

      • If ndigits is positive, rounds to that many decimal places.
      • If ndigits is negative, rounds to the left of the decimal point.

      Behavior with .5

      • Python uses "banker's rounding" (rounds to the nearest even number when exactly halfway between two values).
      print(round(2.5))  # Output: 2
      print(round(3.5))  # Output: 4
      

      Summary

      • round() rounds a number to a specified number of digits.
      • Default is nearest integer.
      • Uses banker's rounding for .5 cases.

      Continue Learning

      Python Variables

      Popular

      Getting Started: Understanding Variables in Python In programming, we often need to store informatio

      Python Classes

      For You

      Building with Blueprints: Understanding Classes and Objects In Python, and many other programming la

      Python Break and Continue

      For You

      Controlling Loop Flow: Understanding break and continue In Python, loops (for and while) normally ex

      Personalized Recommendations

      Log in to get more relevant recommendations based on your reading history.