How to Round to Whole Numbers in Python?

Posted in /  

How to Round to Whole Numbers in Python?
vinaykhatri

Vinay Khatri
Last updated on April 19, 2024

    Python is well known for its numerical computation libraries and modules. As one of the most popular languages for data science , Python provides many methods and functions to handle complex numerical computations. When we write a program in Python, we often have to do numerical calculations using Python math operators .

    Python supports many types of numerical data types , and float and integers are the common ones. A floating-point number can contain a huge decimal point number; however, we can round that floating-point number to a whole number using some of the Python built-in methods.

    In this article, we have mentioned the different Python methods to round off a floating-point number to a whole number. Also, by the end of this article, you will have the answer to the question, "How to round to the whole numbers in Python?" and truncate a long decimal point number to a 2 or 3 decimal point number using the round() function.

    How to Round to Whole Numbers in Python?

    With the help of the round() method, we can round a decimal point number to a whole number or integer. Python also has a built-in math module, which is inherited from the C language, and in this module, we get two methods, ceil() and floor() , that can also be used for rounding up and down values to a whole number or integer.

    All these three methods can round the floating-point number to an integer number, and with the help of these methods, you can also perform Python type conversion , which results in changing the float data type to integer data type. These three methods are:

    1. The round() function rounds up and down a floating or decimal point value.
    2. The math.floor() method rounds down a floating-point value to its next integer value.
    3. The math.ceil() method rounds up a floating-point value to its next integer value.

    1. How to Round to Whole Numbers in Python Using the Round Function?

    The round() function can accept a decimal or floating-point number and returns a rounded up or down integer value for the specified decimal number. The decimal number n.xxx will be rounded up to n+1 if the .xxx is greater than 0.5 .

    Example

    >>> round(0.78)
    1
    
    >>> round(1.78)
    2
    
    >>> round(2.88)
    3

    The decimal number n.xxx will be rounded down to n if the .xxx is less than 0.5 .

    Example

    >>> round(0.45)
    0
    
    >>> round(1.34)
    1
    
    >>> round(2.23)
    2

    The decimal number n.xxx will be rounded to the nearest largest even value n+1 or n if .xxx is equal to 0.5 .

    Example

    >>> round(0.5)
    0
    
    >>> round(1.5)
    2
    
    >>> round(2.5)
    2
    
    >>> round(3.5)
    4
    
    >>> round(4.5)
    4

    2. How to Round to Whole Numbers in Python Using the math.floor() Method?

    Using the round() method, we can round a decimal point number to an integer, but the rounding up and down of the number depends on the decimal point numbers. But if you want to only round down the number without being concerned with the decimal number, you can use the math.floor() method.

    The math.floor() method accepts a floating-point number and rounds it down to an integer or whole number value. In Python programming , you will often be using math.ceil() and math.floor() methods more than the round() function.

    Example of Rounding Down a Floating-point Number to an Integer

    import math
    print("4.5 floor down to: ", math.floor(4.5))
    print("5.6 floor down to: ", math.floor(5.6))
    print("7.9 floor down to: ", math.floor(7.9))
    print("8.10 floor down to: ", math.floor(8.10))
    print("11.62 floor down to: ", math.floor(11.62))
    print("-7.98 floor down to: ", math.floor(-7.98))
    print("-7.11 floor down to: ", math.floor(-7.11))

    Output

    4.5 floor down to:  4
    5.6 floor down to:  5
    7.9 floor down to:  7
    8.10 floor down to:  8
    11.62 floor down to:  11
    -7.98 floor down to:  -8
    -7.11 floor down to:  -8

    How to Round to Whole Numbers in Python Using the math.ceil() Method?

    The math.ceil() method rounds up a floating-point number to an integer or whole number. It rounds up the number to the nearest greater integer.

    Example of Rounding Up a Floating-point Number to the Whole Number or Integer

    import math
    print("4.5 ceil up to: ", math.ceil(4.5))
    print("5.6 ceil up to: ", math.ceil(5.6))
    print("7.9 ceil up to: ", math.ceil(7.9))
    print("8.10 ceil up to: ", math.ceil(8.10))
    print("11.62 ceil up to: ", math.ceil(11.62))
    print("-7.98 ceil up to: ", math.ceil(-7.98))
    print("-7.11 ceil up to: ", math.ceil(-7.11))

    Output

    4.5 ceil up to:  5
    5.6 ceil up to:  6
    7.9 ceil up to:  8
    8.10 ceil up to:  9
    11.62 ceil up to:  12
    -7.98 ceil up to:  -7
    -7.11 ceil up to:  -7

    The Second Parameter of the round(n,d) Method

    The round() function also accepts a second argument that represents the number of decimal digits we want to round up in the given floating-point number.

    Syntax

    round(floating_point_number, number_of_decimal_digits)

    The second parameter for round functions is optional, and by default, its value is None. Also, it accepts a positive integer value, which represents up to which decimal point we want to round up the given value.

    Round Up to Example round Output
    Tense round(11.67, 0) 12.0
    Ones Place round(11.67, 1) 11.7
    Tenth Place round(11.67, 2) 11.67
    Hundredths Place round(11.67324, 3) 11.673
    Thousands Place round(11.67324, 4) 11.6732

    Rounding Up or Down a Python List

    For most cases, we get a list of floating-point numbers, and rounding up or down individual values using for loop could be tedious. But using the Python map() function, we can round up or down every floating-point number present in the list using a single line of code.

    Example of Rounding Up or Down the Python List Using the Round Function

    my_list = [1.67, 8.27, 9.56, 8.25, 9.76, 6.36, 11.23, 10.35, 11.45]
    
    round_up_down = list(map (round, my_list))
    print("The Round up and down list of\n", my_list,"is\n",round_up_down)

    Output

    The Round up and down list of
    [1.67, 8.27, 9.56, 8.25, 9.76, 6.36, 11.23, 10.35, 11.45] is
    [2, 8, 10, 8, 10, 6, 11, 10, 11]

    Example of Rounding Up the Python List Using the math.ceil Method

    import math
    my_list = [1.67, 8.27, 9.56, 8.25, 9.76, 6.36, 11.23, 10.35, 11.45]
    ceil_up = list(map (math.ceil, my_list))
    print("The Round up list of\n", my_list,"is\n",ceil_up)

    Output

    The Round up list of
     [1.67, 8.27, 9.56, 8.25, 9.76, 6.36, 11.23, 10.35, 11.45] is
     [2, 9, 10, 9, 10, 7, 12, 11, 12]

    Example of Rounding Down the Python List Using the math.floor Method

    import math
    my_list = [1.67, 8.27, 9.56, 8.25, 9.76, 6.36, 11.23, 10.35, 11.45]
    floor_down = list(map (math.floor, my_list))
    print("The  Round down list of\n", my_list,"is\n",floor_down)

    Output

    The  Round down list of
     [1.67, 8.27, 9.56, 8.25, 9.76, 6.36, 11.23, 10.35, 11.45] is
     [1, 8, 9, 8, 9, 6, 11, 10, 11]

    Conclusion

    In this Python tutorial, you learned how to round to whole numbers in Python using round(), math.ceil(), and math.floor() methods. You also learned how the round() function works if the decimal point number is 0.5.

    For most of your Python programming, you will be using the math.ceil() and math.floor() methods if you want to round up and down a decimal number to an integer or whole number. The round() method is ideal when you want to round a floating-point number to a specified whole number, and by specifying the second argument in the round function, you can achieve this.

    People are also reading:

    Leave a Comment on this Post

    0 Comments