round(x,n)
x : Input number . 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.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: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:amount = round(49.56789, 2) # Output: 49.57
print(round(12.34567)) # Output: 12
print(round(-12.34567)) # Output: -12
print(round(12.34567, 2)) # Output: 12.35
print(round(-12.34567, 3)) # Output: -12.346
print(round(3.675, 2)) # Expected: 3.68, Actual: 3.67
from decimal import Decimal, ROUND_HALF_UP
value = Decimal('3.675').quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
print(value) # Output: 3.68
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.