Python in its definition allows to handle precision of floating point numbers in several ways using different functions. Most of them are defined under the “math” module. Some of the most used operations are discussed in this article.
1. trunc() :- This function is used to eliminate all decimal part of the floating point number and return the integer without the decimal part.
2. ceil() :- This function is used to print the least integer greater than the given number.
3. floor() :- This function is used to print the greatest integer smaller than the given integer.
The integral value of number is : 3 The smallest integer greater than number is : 4 The greatest integer smaller than number is : 3
There are many ways to set precision of floating point value. Some of them is discussed below.
1. Using “%” :- “%” operator is used to format as well as set precision in python. This is similar to “printf” statement in C programming.
2. Using format() :- This is yet another way to format the string for setting precision.
3. Using round(x,n) :- This function takes 2 arguments, number and the number till which we want decimal part rounded.
The value of number till 2 decimal place(using %) is : 3.45 The value of number till 2 decimal place(using format()) is : 3.45 The value of number till 2 decimal place(using round()) is : 3.45
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- File Handling in Python
- Python | Set 5 (Exception Handling)
- SQL using Python | Set 3 (Handling large data)
- Multiple Exception Handling in Python
- Python | 404 Error handling in Flask
- Python | Handling recursion limit
- Python | Handling no element found in index()
- Handling missing keys in Python dictionaries
- ML | Handling Imbalanced Data with SMOTE and Near Miss Algorithm in Python
- Multilabel Ranking Metrics-Label Ranking Average Precision | ML
- Youtube Data API for handling videos | Set-4
- Youtube Data API for handling videos | Set-5
- Youtube Data API for handling videos | Set-3
- Youtube Data API for handling videos | Set-2
- Youtube Data API for handling videos | Set-1