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
.
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
.
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
There are a large number of ways. For example:
>>> a = "1.351"
>>> b = float(a)
>>> print("%.2f" % (b - b % 0.01))
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