Search code examples
pythondecimal

round decimal numbers in Python


How can I render decimal numbers in Python? For example, in the number 3.14373873 only up to and including the number 4.

I want to round the decimal numbers from a specific place:

As example:

3.14373873 -> 3.14

Solution

  • you can use the round() function:

    https://www.w3schools.com/python/ref_func_round.asp

    Example:

    p = round(50.12345,3)
    print(p)
    

    Output:

     50.123
    

    Example 2:

    # for integers
    print(round(15))
    
    # for floating point
    print(round(51.6))
    print(round(51.5))
    print(round(51.4))
    

    Output:

    15  
    52  
    52  
    51