SoFunction
Updated on 2025-03-10

Basic usage and example code of Python round function

Preface

In Python programming,round()A function is a built-in function that is used to round floating point numbers. This function can approximate floating point numbers, making the results clearer and easier to understand. This article will explore in-depth in Pythonround()Functions, including basic usage, detailed parameter explanation, special case handling and application scenarios, and provide rich sample code to help you better understand and useround()function.

Basic usage

round()The basic syntax of a function is as follows:

rounded_number = round(number, ndigits)

in,numberIt is a floating point number that needs to be rounded.ndigitsis the number of decimal places that are reserved.

Detailed explanation of parameters

1. number

numberIt is a floating point number that needs to be rounded.

Example:

rounded_number = round(3.14159, 2)
print(rounded_number)  # Output: 3.14

In this example, the floating point number is3.14159Rounding was performed and two decimal places were retained.

2. ndigits

ndigitsis the number of decimal places reserved, the default value is 0.

Example:

rounded_number = round(123.456789)
print(rounded_number)  # Output: 123

In this example, the floating point number is123.456789Rounding is performed and 0 decimal places are retained.

Special circumstances handling

1. Rounding Rules

round()The rounding rule of function is based on bankers’ rounding method, also known as even rounding. This means that if the number to be discarded is 5 and the preceding number is even, round to the closest even number. If the preceding number is odd, round up to the nearest even number.

Example:

rounded_number = round(2.5)
print(rounded_number)  # Output: 2
rounded_number = round(3.5)
print(rounded_number)  # Output: 4

In this example,round(2.5)Round the result to 2, andround(3.5)Round the result to 4.

2. Treatment of negative numbers

For negative numbers,round()The function adopts the "round away from zero" method.

Example:

rounded_number = round(-2.5)
print(rounded_number)  # Output: -3
rounded_number = round(-3.5)
print(rounded_number)  # Output: -4

In this example,round(-2.5)Round the result to -3, andround(-3.5)Round the result to -4.

Application scenarios

round()Functions have a wide range of application scenarios in actual programming, and the following are some common use cases:

1. Financial computing

In the financial field, for data such as currency amount or interest rates, it is usually necessary to round to a specified decimal place.

amount = 123.456789
rounded_amount = round(amount, 2)
print(rounded_amount)  # Output: 123.46

2. Data analysis

In data analysis and statistics, large amounts of floating-point data often need to be approximated for better analysis and visualization.

data = [3.14159, 2.71828, 1.61803, 1.41421]
rounded_data = [round(x, 2) for x in data]
print(rounded_data)  # Output: [3.14, 2.72, 1.62, 1.41]

3. Show results

In the user interface or report, displaying results that are accurate to the specified number of decimal places can improve readability and accuracy.

result = 0.123456789
rounded_result = round(result, 4)
print("The result is:", rounded_result)  # Output: The result is: 0.1235

4. Currency calculation

In the financial field, the calculation of currency amounts usually requires rounding to a specified accuracy to ensure the accuracy of the calculation results.

# Application scenarios of currency calculationprice = 19.99
tax_rate = 0.08
total_price = price * (1 + tax_rate)
rounded_total_price = round(total_price, 2)
print("Total price:", rounded_total_price)  # Output: Total price: 21.59

5. Statistical analysis

In data analysis and statistics, processing of floating point data usually requires approximation of it for better analysis and visualization.

# Application scenarios of statistical analysisdata = [1.23, 2.45, 3.67, 4.89]
rounded_data = [round(x, 1) for x in data]
print("Rounded data:", rounded_data)  # Output: Rounded data: [1.2, 2.4, 3.7, 4.9]

6. Calculations with low accuracy requirements

In some scenarios, for calculations with low accuracy requirements, you can useround()Functions approximate the results to simplify the calculation process.

# Calculation with low accuracy requirementsx = 0.3333333333333333
y = 0.6666666666666666
z = round(x + y, 2)
print("Result:", z)  # Output: Result: 1.0

Summarize

Through this article, I have learnedround()The basic usage of functions, detailed parameters, special case handling and application scenarios, and master how to use it in actual programming.round()Functions are a very useful tool in Python that approximate floating-point numbers, making the results clearer and easier to understand. I hope this article can help you better understand and use itround()Functions, perform numbers rounding operations more efficiently in Python programming.