Python Math: Round a specified decimal by setting precision
Python Math: Exercise-40 with Solution
Write a Python program to round a specified decimal by setting precision (between 1 and 4).
Sample Solution:-
Python Code:
import decimal
d = decimal.Decimal('00.26598')
print("Original Number : ",d)
for i in range(1, 5):
decimal.getcontext().prec = i
print("Precision-",i, ':', d * 1)
Sample Output:
Original Number : 0.26598 Precision- 1 : 0.3 Precision- 2 : 0.27 Precision- 3 : 0.266 Precision- 4 : 0.2660
Pictorial Presentation:
Flowchart:

Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to retrieve the current global context (public properties) for all decimal.
Next: Write a Python program to round a specified number upwards towards infinity and down towards negative infinity of precision 4.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Memory Footprint Of An Object:
import sys x = 'farhadmalik' print(sys.getsizeof(x))
60
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook