By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

Decimal Places Incorrect

P: n/a
Hi Folks,

When I run:

print "%0.2f" % ((16160698368/1024/1024/1024),)

I get 15.00

I should be getting 15.05. Can anyone tell me why I'm not?

Thanks, Tom
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Tom Haddon wrote:
Hi Folks,

When I run:

print "%0.2f" % ((16160698368/1024/1024/1024),)

I get 15.00

I should be getting 15.05. Can anyone tell me why I'm not?

Short answer: Integer division.

Long answer: Integer division.

16160698368/1024 = 15781932L
15781932L/1024 = 15412
15412/1024 = 15

Fix:
print "%.02f" % (float(16160698368)/1024/1024/1024,)

15.05
Jul 19 '05 #2

P: n/a
Tom Haddon wrote:
Hi Folks,

When I run:

print "%0.2f" % ((16160698368/1024/1024/1024),)

I get 15.00

I should be getting 15.05. Can anyone tell me why I'm not?


Because you forgot to use "from __future__ import division".

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.