# Python Round

## Python round() function with EXAMPLES

Round() is a built-in function that is accessible in the Python programming language. A float number will be returned to you, which will be rounded to the number of decimal places that were specified as input.

If the number of decimal places to be rounded is not supplied, it is assumed to be zero, and the rounding will be done to the nearest integer.

In this Python tutorial, you will learn how to do the following:

### Syntax: \sround

(float num, num of decimals) = (float num, num of decimals)
Parameters
float num: the float number that will be rounded to the nearest integer.
num of decimals: (optional) The amount of decimals that will be taken into consideration while rounding. When this parameter is not supplied, the value defaults to 0 and the rounding is performed to the nearest integer.
Description
The round() method requires two parameters.

the amount to be rounded as well as the number of decimal places it should take into account while rounding
The second parameter is optional and defaults to 0 if it is not supplied; in this case, it will round to the nearest integer and the return type will also be an integer, as is the case with the first argument.

Whenever the number of decimal places, which is represented by the second parameter, is present, it rounds up to the number of places specified. The type of the return value will be a float.

If the number after the decimal place specified is greater than 5, then + 1 will be added to the final value. If the number after the decimal place specified is less than 5, then the final value will be returned as it is up to the decimal places specified.

### Returning a monetary value

If the num of decimals parameter is not provided, it will return an integer value; if the num of decimals parameter is provided, it will return a float value Remember that the value will be rounded to one if the value after the decimal point is more than five; otherwise, the value will be returned as it is up to the decimal places specified.

What kind of an impact can rounding have? Truncation vs. Rounding: What’s the difference?
The stock exchange market provides the most compelling illustration of the influence of rounding. At one point in time, say 1982, the Vancouver Stock Exchange (VSE) used to truncate the stock prices to three decimal places on each trade, according to the Vancouver Sun.

It was done over 3000 times each day, seven days per week. The accumulating truncations result in a loss of around 25 points every month on the SAT.

### The following illustration illustrates the difference between truncating values and rounding values.

Consider the floating-point figures created in the next section to be stock prices. Right now, I’m generating it for a variety of situations.

Between 0.01 and 0.05, 1,000,000 seconds elapsed.
Examples: arr = [random.uniform(0.01, 0.05) for in range(1000000)] arr = [random.uniform(0.01, 0.05) for in range(1000000)]
To demonstrate the impact of rounding, I’ve built a little piece of code in which you must initially utilise numbers up to only three decimal places, i.e. truncating the number after three decimal places, before moving on to the next step.

In my spreadsheet, I have three totals: the original total, the total derived from truncated numbers, and the difference between the original and truncated values.

The round() method has been used to the same set of numbers up to three decimal places, and the sum and difference between the original and rounded values have been calculated using the round() function.

### Here is an example, as well as the output.

Example 1: Import a random number

truncate(num) is defined as: return int(num * 1000) / 1000.

arr = [random.uniform(0.01, 0.05) for in range(1000000)] for in range(1000000).
If sum num + I is equal to zero, sum truncate is equal to zero. If I is equal to zero, sum num + I is equal to zero. If I is not equal to zero, sum num + I is equal to zero.

make a printout (“Testing by using truncating upto 3 decimal places”)
print(“The original sum is equal to “, sum num), “The original sum is equal to “, sum num)
Print the following: print(“The total using truncate equals “, sum truncate)
print(“The difference between the original and the truncate = “, sum num – sum truncate) print(“The difference between the original and the truncate = “, sum num – sum truncate)

print(“\n\n”)
print(“Testing with round() up to three decimal places,” “Testing with round() up to three decimal places,” “Testing with round() up to three decimal places,” “Testing with round() up to three decimal places,” “Testing with round() up to three decimal places,” “Testing with round() up to three decimal places,” “Testing with round() up to three decimal places,” “Testing with round() up to three decimal places
sum num1 equals 0 sum num2
for each I in arr: _truncate1 = 0
sum
_num1 = sum num1 + I sum truncate1 = round(sum truncate1 + I 3) sum truncate1 = round(sum truncate1 + I 3)

print(“The original sum is =”, sum num1) print(“The original sum is =”, sum num1)
print(“The total using round = “, sum truncate1) print(“The total using round = “, sum truncate1) print(“The total using round = “, sum truncate1) print(“The total using round = “, sum truncate1) print(“The total using round = “, sum truncate1) print(“The total using round = “, sum truncate1) print(“The total using round = “, sum truncate1)
print(“The difference from the original – round =”, sum num1 – sum truncate1) print(“The difference from the original – round =”, sum num1 – sum truncate1)
Output:
Testing by truncating numbers to up to three decimal places
The original sum is equal to 29985.958619386867 dollars.
Using truncate, the total comes to 29486.057.
The difference between the original and the truncate value is 499.9016193868665.

1. Using round() to round numbers up to three decimal places was used in the testing.
2. The original sum is equal to 29985.958619386867 dollars.
3. The total utilising rounding is 29985.912 dollars.
4. The difference between the original and the round value is 0.04661938686695066.
5. The difference between the original and after truncating is 499.9016193868665, and the difference between the original and after rounding is 0.04661938686695066.

The difference appears to be significant, and the example demonstrates how the round() method can assist in calculating calculations that are close to correctness.

As an illustration, rounding float numbers
In this programme, we will demonstrate how rounding words on floating values affects the results of calculations.

### # This is a testing round ()

float num1 = 10.60 # # # # # # # # The answer will be adjusted to 11 in this case since the number after the decimal point is 6, which is greater than 5.

float num2 = 10.40 # # # # # # # The value will be rounded to ten in this case since the number after the decimal point is four, which is equal to five.

10.3456 is the value of the float num3 variable. The number will be 10.35 in this case because the value >=5 after the two decimal points.

float num4 = 10.3445 # # # # # #

The value will be 10.34 in this case since the value is 5 after the two decimal points.

print(“The rounded value without the num of decimals is:”, round(float num1)) print(“The rounded value without the num of decimals is:” print(“The rounded value without the num of decimals is:” print(“The rounded value without the num of decimals is:” print(“The rounded value without the num of decimals is:” print(“The rounded value without the num
print(“The rounded value without the num of decimals is:”, round(float num2)) print(“The rounded value without the num of decimals is:”, round(float num2))
The rounded value with the number of decimals as 2 is: print(“The rounded value with the number of decimals as 2 is:”, round(float num3, 2))
The rounded value with the number of decimals as 2 is: print(“The rounded value with the number of decimals as 2 is:”, round(float num4, 2))
Output:

It’s 11 if you don’t count the number of decimal places.
In the absence of num of decimals, the rounded value is: 10.
Using the rounded value and a num of decimals of 2, the result is: 10.35.
If you round to two decimal places, you get the following result: 10.34.
As an illustration, consider the following: rounding integer values
You will get the same number back if you call round() on an integer value; otherwise, you will get the same number back with no modifications.

### # Putting round() to the test on an integer

num is equal to 15

print(“The output is”, round(num)) Output: print(“The output is”

The result is a number of fifteen.
As an illustration, consider the following: rounding on negative numbers
Here are a couple of illustrations of how rounding works on negative integers to illustrate the point.

### # This is a testing round ()

num = -2.8 num1 = -1.5 print(“The value after rounding is”, round(num)) print(“The value after rounding is”, round(num1)) print(“The value after rounding is”, round(num2)) print(“The value after rounding is”, round(num1))
Output:

Python testround.py is located in the C:pythontest directory.
After rounding, the result is a negative number of -3.
After rounding, the result is a negative number.
Round Numpy Arrays, for instance.
What is the best way to round numpy arrays in Python?

If we use the numpy module and its methods, we may address this problem, as demonstrated in the example below, by using the numpy.round() and numpy.around() methods, respectively.

Numpy.round is being used ()

### # Import numpy as np for the purpose of testing round()

[-0.341111, 1.455098989, 4.232323, -0.3432326, 7.626632, 5] arr = [-0.341111, 1.455098989, 4.232323, -0.3432326, 7.626632, 5] .122323]

np.round + arr1 = np.round (arr, 2)

print(arr1) \sOutput:

Python testround.py is located in the C:pythontest directory.
[-0.34 1.46 4.23 -0.34 7.63 5] [-0.34 1.46 4.23 -0.34 7.63 5] .12]
Additionally, we can use the numpy.around() function, which produces the same result as the one given in the example below.

As an illustration, consider the Decimal Module.
In addition to the round() function, the Python programming language includes a decimal module that aids in the more precise handling of decimal integers.

Rounding types are included with the Decimal module, as seen in the table below:

ROUND CEILING: The ceiling will continue to round towards Infinity.
ROUND DOWN: It will round the value down to the nearest tenth.
ROUND FLOOR: It will go around in circles until it reaches -Infinity.
ROUND HALF DOWN: It will round to the nearest half-point when it approaches zero.
ROUND HALF EVEN: It will round to the nearest even integer, with the value going to the nearest even integer, and so on.
It will round to the nearest half-point with the value increasing as it moves away from zero.
ROUND UP: It will round the value up to the nearest whole number, regardless of how far it is from zero.
Quantize() is a method in the decimal language that assists in rounding to a specified number of decimal places. You can specify the rounding to be used, as seen in the example below, by passing in a fixed number of decimal places.

As an illustration, the round() and decimal functions are used.

round number = 15.456 in the import decimal round number

final val = round(round num, 2) final val = round(round num, 2)

### # Using the decimal module final val1 = decimal to achieve this.

• Decimal(round num).
• quantize(decimal.
• Decimal(‘0.00’), rounding=decimal, decimal(‘0.00’).
• ROUND CEILING) final val2 = decimal is a rounding error.
• Decimal(round num).
• quantize(decimal.
• Decimal(‘0.00’), rounding=decimal, decimal(‘0.00’).
• (ROUND DOWN) final val3 = decimal (ROUND DOWN)
• Decimal(round num).
• quantize(decimal.
• Decimal(‘0.00’), rounding=decimal, decimal(‘0.00’).
• The round floor (ROUND FLOOR) final val4 is a decimal.
• Decimal(round num).
• quantize(decimal.
• Decimal(‘0.00’), rounding=decimal, decimal(‘0.00’).
• ROUND HALF DOWN) final val5 = decimal (ROUND HALF DOWN)
• Decimal(round num).
• quantize(decimal.
• Decimal(‘0.00’), rounding=decimal, decimal(‘0.00’).
• Rounded half-even) final val6 = decimal (Rounded half-even)
• Decimal(round num).
• quantize(decimal.
• Decimal(‘0.00’), rounding=decimal, decimal(‘0.00’).
• ROUND HALF UP) final val7 = decimal (ROUND HALF UP)
• Decimal(round num).
• quantize(decimal.
• Decimal(‘0.00’), rounding=decimal, decimal(‘0.00’).

ROUND UP)

final val = print(“Using round(),” final val)
print(“Using Decimal – ROUND CEILING “,final val1) print(“Using Decimal – ROUND DOWN “,final val2) print(“Using Decimal – ROUND CEILING “,final val1) print(“Using Decimal – ROUND DOWN “,final val2)
Print the following: print(“Using Decimal – ROUND FLOOR”,final val3)
print(“Using Decimal – ROUND HALF DOWN “,final val4) print(“Using Decimal – ROUND HALF DOWN “,final val4)
print(“Using Decimal – ROUND HALF EVEN “,final val5) print(“Using Decimal – ROUND HALF UP “,final val6) print(“Using Decimal – ROUND HALF EVEN “,final val6) print(“Using Decimal – ROUND HALF EVEN “,final val6) print(“Using Decimal – ROUND HALF EVEN “,final val6)
print(“Using Decimal – ROUND UP “,final val7) print(“Using Decimal – ROUND UP “,final val7)
Output:

Using round() 15.46 Using Decimal – ROUND CEILING 15.46 Using Decimal – ROUND DOWN 15.46 Using round() 15.46 Using Decimal – ROUND DOWN The ROUND FLOOR method is used at 15.45. ROUND HALF DOWN – 15.45 Using Decimal Numbers 15.46 Using Decimal – ROUND HALF EVEN as a Rounding Rule ROUND HALF UP is used in place of decimal in 15.46. 15.46 Using Decimal – ROUND UP Using Decimal 15.46
Summary:
Round(float num, Num of decimals) is a built-in function in the Python programming language. It will return a float number that will be rounded to the number of decimal places specified in the input parameter.
float num: the float number that will be rounded to the nearest integer.
Num of decimals: It is the number of decimals that will be taken into account while rounding.
If the num of decimals parameter is not provided, it will return an integer value; if the num of decimals parameter is provided, it will return a float value
Perhaps you’d be interested in this: PyUnit Tutorial: Python Unit Testing Framework (with Example)
Call, indentation, arguments, and return values are all examples of Python functions.
Exemples of using Python with MySQL connectivity: database and table.
Python’s New Line of Code: How to print in Python without using a newline
Remove an entry from a list of Python objects Remove [clear] [pop] [remove] [delete] [pop] [delete]
Submit a Bug Report Previous Next
Tutorials that are the best
Our Mission Our Vision Our Values Our Mission Our Values Our Values Our Values Our Values Our Values Our Values Our Values Our Values
We invite you to advertise with us.
Write a Letter to Us
Get in Touch With Us
Python \sTesting
Hacking
Suggestions for a Professional Career
Using the SAP Career Suggestion Tool, you can pursue a career in software testing.

Interesting \seBook \sBlog \sQuiz
SAP Java SQL Execute SAP eBook SAP Java SQL Execute online
Java may be executed online.
Javascript is being executed.
HTML code should be executed.
Python Selenium Cryptocurrency Virtual Private Networks (VPNs)
Guru99 2022 is the owner of the copyright. Privacy Statement | Affiliate Disclaimer | Terms of Service