Search code examples
python-3.xpandasnumpyfloating-pointgoogle-finance

Limiting float value in for loop Python


I'm working on banking project, where my team asks me to limit all float values to .2 precision.

My dataSet.head()

bank_stocks dataset

Goal: To find max of all stocks comparatively

My code & error i got

My present output:

Bank Ticker
BAC       54.900002
C            564.099976
GS         247.919998
JPM       70.080002
MS         89.300003
WFC      58.520000
dtype: float64

My expected output:

Bank Ticker
BAC       54.90
C            564.10
GS         247.91
JPM       70.08
MS         89.30
WFC      58.52
dtype: float64

Please help me with this!


Solution

  • You make a wrong use of "{:.2f}" in your print statement, you should use .format() to format your float.

    You can use print("{:.2f}".format(some float)) to print a float with 2 decimals as explained here.