Mounarajan Mounarajan - 5 months ago 8
Python Question

How to seperate last two digits with dot in Python

I always want to separate the last two digits with a decimal point . (dot)L

print("{:.2f}".format(300));


Output:

300.00


Expected output:

3.00


Next

print("{:.2f}".format(333333300));


Output:

333333300.00


Expected output:

3333333.00


How can I change my code so that I get the expected output instead?

Answer

300.00 is a representation of number 300. 3.00 is a representation of number 3. Those are two different values. There is no way Python will represent 300 as 3.

Assuming you really want to print 3.00 (which for pretty much everyone around means 3, not 300), then you should divide 300 by 100.

print("{:.2f}".format(300/100.0))
Comments