round(): Rounding Numbers to Specified Decimals

round(x,n)
x : Input number .
n :(optional ) Input number of digits after decimal .
Returns float or integer after rounding the input number upto the given decimal.
print(round(12.34567))     # 12
print(round(-12.34567))    # -12
print(round(12))           # 12
print(round(-12))          # -12
print(round(12.34567,2))   # 12.35
print(round(-12.34567,3))  # -12.346
The `round()` function in Python is used to round a floating-point number to a specified number of decimal places. This function is commonly applied in financial calculations, formatting outputs, and data analysis.

To get better precision, decimal module can be used. Check this line. Expected output is not 3.68
print(round(3.675,2)) # 3.67 
Rounding to Nearest hundreds, thousands:
print(round(1245, -2))  # Output: 1200
print(round(1245, -3))  # Output: 1000
print(round(1295, -2))  # Output: 1300
print(round(1745, -3))  # Output: 2000
Rounding Floats in Lists:
Rounds all values in a list to two decimal places.
values = [1.5678, 2.456, 3.14159]
rounded_values = [round(v, 2) for v in values]
print(rounded_values)  # Output: [1.57, 2.46, 3.14]
Use Case: Financial Calculations: In banking applications, rounding is critical to ensure monetary precision:
This ensures consistency in calculations involving currency.
amount = round(49.56789, 2)  # Output: 49.57

Example 1: Basic Rounding

print(round(12.34567))   # Output: 12
print(round(-12.34567))  # Output: -12

Example 2: Specifying Decimal Places

Control the decimal places by setting the `digits` argument.
print(round(12.34567, 2))   # Output: 12.35
print(round(-12.34567, 3))  # Output: -12.346

Example 3: Rounding and Precision Limits

Due to floating-point precision, results can be slightly unexpected.
print(round(3.675, 2))  # Expected: 3.68, Actual: 3.67

Using Decimal for Greater Precision

For critical precision, Python’s `decimal` module provides better accuracy.
from decimal import Decimal, ROUND_HALF_UP
value = Decimal('3.675').quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
print(value)  # Output: 3.68

Applications of round()

  • Financial Calculations: Ensures consistency in monetary values by rounding to two decimal places.
  • Data Formatting: Clean up outputs for user readability.
  • Precision Control: Manage significant figures in scientific calculations.


Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer