Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Investment calculator – returned number format issue

I’ve created a function, which returns DataFrame with 3 columns – Year, Principal amount and End balance. It works fine, however the format number of End balance is weird, but I’m not sure, what I’ve done wrong.

If the input is investment_calculator(15000,7,10). It returns End balance in correct format:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

However if it exceeds particular amount, e.g. investment_calculator(15000,10,10). The format of End balance is different, although nothing’s changed:

enter image description here

Function:

def investment_calculator(amount,years,interest):
    """
    Investment calculator calculates returned amount.

    Args:
        amount: monthly invested amount
        years: number of years
        interest: average yearly interest

    Returns:
        DataFrame
    """
    col_names = ['Year', 'Principal', 'End balance']
    df = pd.DataFrame(columns = col_names)

    i = 1
    while i <= years:
        invested_amount = (amount * 12) * i
        year = datetime.datetime.now().year + (i - 1)
        
        
        if i == 1:
            investment = (amount * 12) * (1 + (interest / 100))
            new_row = {'Year':year, 'Principal':invested_amount, 'End balance':investment}
            df = df.append(new_row, ignore_index=True)
        else:
            investment = (df.iloc[-1,2] + (amount * 12)) * (1 + (interest / 100))
            new_row = {'Year':year, 'Principal':invested_amount, 'End balance':investment}
            df = df.append(new_row, ignore_index=True)
            
        i += 1
        
    return df

Any ideas, what’s wrong, please?

>Solution :

I am sure you know your values are simply displayed using scientific notation. If its bother you, you can set a different default format, for instance:

pd.options.display.float_format = '{:.2f}'.format
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading