0

Given the string a="1.351", how do I round down to 2 decimal points? I tried:

a = "1.351"
b = "%0.2f" % float(a)
c = math.floor(float(b))  
print c  # gives me an output of 1.0

Ideally I would like an output of 1.30.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Bob
  • 25
  • 2
  • 6
  • Why do you think `1.30` is the correct output? Have you tried using `round`? – jonrsharpe Dec 21 '16 at 11:06
  • 2
    Possible duplicate of [Limiting floats to two decimal points](http://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points) – jonrsharpe Dec 21 '16 at 11:07

3 Answers3

3

If you meant to say the expected output was:

1.35

Then you can try the following:

a = 1.351
print math.floor(a*100)/100
gom1
  • 150
  • 8
1

There are a large number of ways. For example:

>>> a = "1.351"
>>> b = float(a)
>>> print("%.2f" % (b - b % 0.01))
Dmitry
  • 2,026
  • 1
  • 18
  • 22
0

Try this code

a = "1.351"
float(int(float('{:.2f}'.format(float(a)))*10))/10

Output :

1.3

for b= b="1254.25465"

output = 1254.2

khelili miliana
  • 3,730
  • 2
  • 15
  • 28