Book Image

Python for Finance

By : Yuxing Yan
Book Image

Python for Finance

By: Yuxing Yan

Overview of this book

Table of Contents (20 chapters)
Python for Finance
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Choosing appropriate precision


The default precision for Python has 16 decimal places as shown in the following example. This is good enough for most finance-related problems or research:

>>>7/3
2.3333333333333335

We could use the round() function to change the precision as follows:

>>>payment1=3/7
>>>payment1
0.42857142857142855
>>>payment2=round(y,5)
>>>payment2
0.42857

Assume that the units for both payment1 and payment2 are in millions. The difference could be huge after we apply the round() function with just two decimal places! If we use one dollar as our unit, the exact payment is $428,571. However, if we use millions instead and apply two decimal places, we end up with 430,000, which is shown in the following example. The difference is $1,429:

>>>payment1*10**6
428571.4285714285
>>>payment2=round(payment1,2)
>>>payment2
0.43
>>>payment2*10**6
430000.0