Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to round down to 2 decimals a float using Python?
In this article, we will show you how to round off a floating number upto 2 decimals in python. Below are the various methods to accomplish this task:
Using round() function
Using format() function
Using Decimal Module
Using ceil() function
Using round() function
The round() function gives a floating point number with a specified number of decimals which is the rounded version of the specified number.
The function will return the nearest integer because the default value for the number of decimals is 0.
Syntax
round(number, digits)
Parameters
number(required)- a number that should be rounded
digits(optional)- up to the number of decimals to be rounded. 0 is the default.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input number.
Pass input number, 2 (decimal value) as arguments to the round() function to round the input number upto the 2 decimal places.
Print the rounded value of the input floating-point number upto 2 decimals.
Example
The following program returns the rounded value of the input floating-point number upto the 2 decimals using the round() function-
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># input floating-point number</span> inputNumber <span class="token operator">=</span> <span class="token number">3.367824</span> <span class="token comment"># rounding the number up to 2 decimal places</span> roundedNumber <span class="token operator">=</span> <span class="token builtin">round</span><span class="token punctuation">(</span>inputNumber<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment"># print the rounded value of floating-point number up to 2 decimals</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Rounding 3.367824 upto 2 decimal places:"</span><span class="token punctuation">,</span> roundedNumber<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
On executing, the above program will generate the following output -
Rounding 3.367824 upto 2 decimal places: 3.37
Using format() function
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input floating-point number.
Use the format() function (It gives back a formatted version of the input value that has been specified by the format specifier) to round the number upto the 2 decimal places by passing the input number, format (upto to the 2 decimals) as arguments to it.
Example
The following program returns the rounded value of the input floating-point number upto the 2 decimals using the format() function-
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># input floating-point number</span> inputNumber <span class="token operator">=</span> <span class="token number">4.56782</span> <span class="token comment"># rounding the number upto 2 decimal places</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Rounding 4.56782 upto 2 decimal places:"</span><span class="token punctuation">,</span> <span class="token builtin">format</span><span class="token punctuation">(</span>inputNumber<span class="token punctuation">,</span><span class="token string">".2f"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
On executing, the above program will generate the following output -
Rounding 4.56782 upto 2 decimal places: 4.57
Using Decimal Module
Python's decimal module helps to increase the accuracy of floating-point numbers. To use this decimal module, we must import it.
decimal.Decimal(floatnumber) ? Gives a decimal value of 50 digits as default.
Here we use value.quantize(decimal.Decimal('0.00')) to round upto 2 digits after the decimal point.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Use the import keyword to import the decimal module.
Create a variable to store the input floating-point number.
Convert the given floating-point number using the Decimal() function of the decimal module.
Round the number upto 2 digits (here 2 decimal palace . hence we give 2 zeros after the decimal point) after the decimal point using the value.quantize(decimal.Decimal()) function.
Print the input floating-point number
Print the rounded value of the input number upto the 2 decimal places.
Example
The following program returns the rounded value of the input floating-point number upto the 2 decimals using the decimal module -
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># importing decimal module</span> <span class="token keyword">import</span> decimal <span class="token comment"># input floating-point number</span> inputNumber <span class="token operator">=</span> <span class="token number">35.65782</span> <span class="token comment"># Converting the given number to decimal</span> decimalValue <span class="token operator">=</span> decimal<span class="token punctuation">.</span>Decimal<span class="token punctuation">(</span>inputNumber<span class="token punctuation">)</span> <span class="token comment"># rounding the number upto 2 digits after the decimal point</span> roundedNumber <span class="token operator">=</span> decimalValue<span class="token punctuation">.</span>quantize<span class="token punctuation">(</span>decimal<span class="token punctuation">.</span>Decimal<span class="token punctuation">(</span><span class="token string">'0.00'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># printing the input floating-point number</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Input floating-point number: "</span><span class="token punctuation">,</span> inputNumber<span class="token punctuation">)</span> <span class="token comment"># printing the rounded value of the input number upto 2 decimal places</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Rounding 35.65782 upto 2 decimal places:"</span><span class="token punctuation">,</span> roundedNumber<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
On executing, the above program will generate the following output -
Input floating-point number: 35.65782 Rounding 35.65782 upto 2 decimal places: 35.66
Using ceil() function
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Use the import keyword to import the math module.
Create a variable to store the input floating-point number.
Use the ceil() function(returns a ceiling value of the number i.e., the smallest integer greater than or equal to the number), to round the number upto the 2 decimal places and print the resultant number.
If you want to round upto 1, 2, 3, 4...decimals then multiply and divide by # 10, 100, 1000, 10000 numbers respectively
Example
The following program returns the rounded value of the input floating-point number upto the 2 decimals using the ceil() function -
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># importing math module</span> <span class="token keyword">import</span> math <span class="token comment"># input floating-point number</span> inputNumber <span class="token operator">=</span> <span class="token number">4.56782</span> <span class="token comment"># rounding the number upto 2 decimal places using ceil() function</span> <span class="token comment"># If you want to round upto 1, 2, 3, 4...decimals then multiply and divide by</span> <span class="token comment"># 10, 100, 1000, 10000 numbers respectively</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Rounding 4.56782 upto 2 decimal places:"</span><span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span>ceil<span class="token punctuation">(</span>inputNumber<span class="token operator">*</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">100</span><span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
On executing, the above program will generate the following output -
Input floating-point number: 35.65782 Rounding 4.56782 upto 2 decimal places: 4.57
Conclusion
In this article, we learned how to round off a given floating number to two decimal places in Python using four different methods. Using the ceil function and some mathematical logic, we learned how to round to two decimal places. We also learned how to use the decimal module to convert a given floating point number to a decimal and quantize it.
