How to display two decimal places in Python when a number is perfectly divisible?
12:39 27 Jan 2022

I am supposed to print a number up to two decimal places without rounding off. I have used the below code for this purpose

import math
a = 1.175
print(math.floor(a*100) / 100)

The output we get is:

1.17

Notice that the value has two decimal places and is not rounded.

The 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 a is perfectly divisible:

a = 25/5
print(math.floor(a*100) / 100)

The output displayed now is

5.0

Notice only one decimal place is printed.

What must be done to rectify this bug?

python floating-point decimal