Alexis Eggermont
Alexis Eggermont

Reputation: 8145

Python rounds decimal automatically

I'm trying to get a daily rate from an annual rate of 26%.

In Python, 1.26**(1/365) gives me 1.0

In Excel, 1.26^(1/365) gives me 1.000633, which is what I want.

Why is Python doing this and how can I get a more accurate result?

Upvotes: 1

Views: 204

Answers (1)

Alex Riley
Alex Riley

Reputation: 176770

You are using Python 2.x so 1 / 365 is zero (division of two integers returns an integer). Anything to the power of zero is 1.

You need to use true division; you could make one of the numbers a floating point number to trigger this:

>>> 1.26 ** (1.0 / 365)
1.000633383299703

Upvotes: 6

Related Questions