Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Convert float to int and round to N decimal places - Python Program

To convert a float to an integer and round it to a specific number of decimal places in Python, you can combine the round() function with the int() function.

Steps:

  • Convert to integer:

Use the int() function to convert the rounded float into an integer. This will effectively truncate any remaining decimal places after rounding. 

  • Round the float:

Use the built-in round() function to round the float to the desired number of decimal places.

    val = 12.6789 print("As integer:", int(val)) print("Rounded to 2 decimals:", round(val, 2))

    Output

     
    OUTPUT  :
    As integer: 12
    Rounded to 2 decimals: 12.68
    

    int() truncates decimals. round() can round to any number of decimal places.