In the previous article, we have discussed Python Program for reversed() Function
round() Function in Python:
The round() function returns a floating-point number with the specified number of decimals that is a rounded version of the specified number.
The function will return the nearest integer if the number of decimals is set to 0.
Syntax:
round(number, digits)
Parameters
number: This is required. It is the number that should be rounded.
digits: This is Optional. When rounding a number, the number of decimals to use(up to how many digits to be rounded after decimal) The default value is 0.
Return Value:
The function round() returns the
If digits is not provided, the nearest integer to the given number is used; otherwise, the number is rounded off to digits.
Examples:
Example1:
Input:
Given number = 10.5678 Given no of digits = 2
Output:
The rounded value of the given number 10.5678 upto 2 digits = 10.57
Example2:
Input:
Given number = 7.8
Output:
The rounded value of the given number 7.8 = 8
Program for round() Function in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Give the digits (up to how many digits to be rounded after the decimal)as static input and store it in another variable.
- Get the rounded value of the given number by passing the given number and given digits as arguments to the round() function.
- Store it in another variable.
- Print the rounded value of the given number.
- The Exit of Program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvn_numb = 10.5678 # Give the digits (up to how many digits to be rounded after the decimal)as # static input and store it in another variable. gvn_digitss = 2 # Get the rounded value of the given number by passing the given number # and given digits as arguments to the round() function. # Store it in another variable. numbraftr_round = round(gvn_numb, gvn_digitss) # Print the rounded value of the given number. print("The rounded value of the given number", gvn_numb, "upto", gvn_digitss, "digits = ", numbraftr_round)
Output:
The rounded value of the given number 10.5678 upto 2 digits = 10.57
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the number as user input using the float(input()) function and store it in a variable.
- Get the rounded value of the given number by passing the given number as an argument to the round() function.
- Store it in another variable.
- Print the rounded value of the given number.
- The Exit of Program.
Below is the implementation:
# Give the number as user input using the float(input()) function and store it in a variable. gvn_numb = float(input("Enter some random number = ")) # Get the rounded value of the given number by passing the given number # as an argument to the round() function. # Store it in another variable. numbraftr_round = round(gvn_numb) # Print the rounded value of the given number. print("The rounded value of the given number", gvn_numb, "= ", numbraftr_round)
Output:
Enter some random number = 7.8 The rounded value of the given number 7.8 = 8
Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.