How do I print a number with only two decimal places in Python?

You want to print a number with only two decimal places in python.

    print only two decimal places by using.

    1. Format method
    2. % formatting
    3. round method

    and etc.

    Let’s see an example of it.

    By using Format Method:

    num = 16.52907534 # Take a float number.

    print(“{:.2f}”.format(num))


    By using % formatting:

    print("%.2f" % num)
    By using round method:
    print("{:.2f}".format(round(num,2)))

     Output:

    16.52

    16.52

    16.52

    Comments

    Popular posts from this blog

    proassignment help backlink data index 41

    Programming shark backlink data index 67

    AHM Backlink Data Index 95