Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to display two decimal points in python, when a number is perfectly divisible?

Currently I am trying to solve a problem, where I am supposed to print the answer upto two decimal points without rounding off. I have used the below code for this purpose

import math
a=1.175                            #value of a after some division
print(math.floor(a*100)/100)

The output we get is:

1.17                              #Notice value which has two decimal points & not rounded

But the real problem starts when I try to print a number which is evenly divisible, after the decimal point only one zero is displayed. I have used the same code as above, but now

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

a=25/5                                   #Now a is perfectly divisible
print(math.floor(a*100)/100)

The output displayed now is

5.0                                      #Notice only one decimal place is printed

what must be done rectify this bug?

>Solution :

The division works and returns adequate precision in result.

So your problem is just about visualization or exactly:

  • string-representation of floating-point numbers

Formatting a decimal

You can use string-formatting for that.
For example on Python 3, use f-strings:

twoFractionDigits = f"{result:.2f}"

or print(f"{result:.2f}")

See also:

Try on the Python-shell:

Python 3.6.9 (default, Dec  8 2021, 21:08:43) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> a=1.175                            #value of a after some division
>>> result = math.floor(a*100)/100
>>> result
1.17
>>> print(result)
1.17
>>> a=25/5                                   #Now a is perfectly divisible
>>> result = math.floor(a*100)/100
>>> result
5.0
>>> print(result)
5.0
>>> print(f"{result:.2f}")
5.00

Formatting a decimal as percentage

Similar you can represent the ratio as percentage:
print(f"{result:.2f} %")

prints:

5.00 %

A formatting shortcut for percentage can be:
print(f"{25/100:.2%}")
Which converts the result of 25/100 == 0.25 to:

25.00%

Note: The formatting-literal .2% automatically converts from ratio to percentage with 2 digits after the decimal-point and adds the percent-symbol.

Formatting many decimals in fixed width (leading spaces)

Another example is to print multiple decimals, like in a column as right-aligned, so you can easily compare them.

Then use string-formatting literal :6.2f to add leading spaces (here a fixed-width of 6):

>>> print(f"{result:6.2f} %")
  5.00 %
>>> print(f"{100/25*100:6.2f} %")
400.00 %
>>> print(f"{25/100*100:6.2f} %")
 25.00 %

All this can also be done using %-formatting (compatible with Python before 3.6) as in theherk’s answer.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading