Thứ Tư, Tháng Hai 12, 2025
spot_img
Homeround() function in Python - QuanTriMang.com

round() function in Python – QuanTriMang.com

Rounding in Python not difficult. The article below will guide you in detail How to use round function in Python to round numbers.

Python is the most popular programming language today. With many businesses turning to Python's powerful data science ecosystem for data analysis, understanding how to avoid introducing bias into datasets is extremely important. If you have studied some metrics, you may be familiar with terms like reporting bias, selection bias, and sampling bias. There is another type of bias that plays an important role when you deal with numerical data. That's rounding error.

Understanding how rounding works in Python can help you avoid skewing your data set. This is an important skill. After all, drawing conclusions from biased data can lead to costly mistakes. If you don't want that, let's learn about rounding numbers in Python using the round() function.

round() function in Python rounds a given number, returning it as a floating point number, with the specified number of digits after the decimal point. The default number after the comma is 0, meaning the function will return the nearest integer.

Syntax of round() function in Python:

round(number[, ndigits])

Parameters of round() function

The round() function has 2 parameters:

  • number: The number you want to round.
  • ndigits: The number of digits after the comma you want to use to round the number. Default is 0.

Return value from round()

  • If not ndigitsthe function will return the nearest integer.
  • If 2 parameters are provided, the function returns number with ndigits decimal number after comma.

Example 1: How does round() work?

Example passing only the first parameter:

# không có tham số ndigits 
print(round(10))
print(round(10.7))
print(round(5.5))
print(round(5.4))
print(round(6.8))
print(round(0.1))
print(round(0.7))

When you run the program, the output will be:

10
11
6
5
7
0
1

Example 2: Round the number to the given ndigits position

For example, passing both parameters:

print(round(2.665, 2))
print(round(50.25556, 2))
print(round(100.000056, 3))
print(round(80.23456, 2))
print(round(122.145,5))

When you run the program, the output will be:

2.67
50.26
100.0
80.23
122.145

Errors and exceptions

TypeError: This error arises in cases when there is any data other than numbers in the parameter.

print(round("a", 2))

Result:

Runtime Errors:
Traceback (most recent call last):
File "/home/ccdcfc451ab046030492e0e758d42461.py", line 1, in
print(round("a", 2))
TypeError: type str doesn't define __round__ method

Practical application:

One of the common uses of the rounding function is to handle mismatches between fractions and decimals.

One way to round a number is to shorten all three numbers to the right of the decimal point when converting 1/3 to a decimal. Most of the time, you will use rounded numbers 0.33 or 0.333 when you need to work with 1/3 in decimals. In fact, you should always only work with the two or three numbers to the right of the decimal point when there is no exact equivalent to the fraction in decimal form. How to display 1/6 in decimal? Remember to round numbers!

For example:

# practical application
b = 1/3
print(b)
print(round(b, 2))

Result:

0.3333333333333333
0.33

Some other practical applications of the rounding function in Python

  • Round up the bill – The total bill payment is usually calculated as an integer value, so the round() function can be used to round the amount in the bill.
  • Rounding up scientific data – Scientific data has high precision, so it needs to be in a required format with certain integers and real numbers. This precision can be achieved with the help of round() function.
  • Processing technical data – Theoretical data in engineering may need adjustment to be usable in practical applications. The round() function can help round the application's values ​​in such cases.

Round function() Just a few notes like that. Remember to practice regularly with Python exercises.

See more:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments