4

I want to Calculate up to two decimal places in my Calculations but I can't

For example:

a = 93
b = 1
c = float(93/1)
print c

I want to print like this :

93.00

But it print like this :

93.0

Is there any function to do this or is there any way to do this?

Lukas Graf
  • 30,317
  • 8
  • 77
  • 92

2 Answers2

3

use an explicit format to that float

print("%.2f" % round(c,2))

test it on a jupyter

https://try.jupyter.org/


enter image description here

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

First you have to change your code to

c = float(93.0/1.0)

This is because you are using integer division.

#  eg. 93/2 == 46
#  but 93.0/2 == 46.5 and
#      93.0/2.0 == 46.5
print c

then to print 2 decimal places you have to use python formatting

print "%.2f" % c
# 93.00
Mitiku
  • 5,337
  • 3
  • 18
  • 35