Can someone please explain these results to me and why they are different:
>>> 7339520/(1024.0*1024)
6.99951171875
>>> print "%0.3f" % (7339520/(1024.0*1024))
7.000
>>>
You are telling to round to 3 decimals in second.
What if i just wanted to tell it to truncate to 3 decimal points? That is what i wanted to do in the first place. :(
Use the second version then, I do not see any problem.
The issue is that i want print "%0.3f" % (7339520/(1024.0*1024)) to give me 6.99 instead of 7. Is there an easy way to do this ?
I did not find exact match as round rounds numbers, does not cut. It is simple to write small function though:
>>> def cut(n, decimals):
return int(n*10**decimals)/10**decimals
>>> cut(7339520/(1024.0*1024), 3)
6.999
>>> cut(7339520/(1024.0*1024), 2)
6.99
>>>
I did not find exact match as round rounds numbers, does not cut. It is simple to write small function though:
>>> def cut(n, decimals): return int(n*10**decimals)/10**decimals >>> cut(7339520/(1024.0*1024), 3) 6.999 >>> cut(7339520/(1024.0*1024), 2) 6.99 >>>
you can use math.trunc() instead of int().
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.