How to Round Numbers in Python

In Python, there may be situations when we need to convert a decimal fraction to a whole number. Rounding a value is the practice of replacing a value with another value that is almost equivalent to the original value. For instance, the number 3.5 will be rounded to either 3 or 4. Rounding makes the values simpler to understand. Python provides a built-in method called round(). By using the round() function, a float number will be returned and rounded to the specified decimal points. This article will teach you how to use the round() function as well as several rounding methods.

What is the Floating Type in Python?

The floating number is represented by the datatype float in Python. A decimal point separates the fractional and integer parts. Its Python representation is a 64-bit double-precision float data type. Here are a few examples of float values: 2.21, 1.41, 23.50, 68.12304, and 1.12e4. These examples show float values in scientific notation, where the decimal part is called the mantissa and the exponential is called the exponent.

How Can Numbers be Rounded in Python?

In python, several methods can be used to round a number. We will discuss a few of them:

Let’s start with the round() function which is an inbuilt function provided by python specifically to help us in rounding numbers.

Rounding Number Using Round() Function

There is a function in Python called round() that allows us to round a number. Since the function is already included in the Python library, no additional imports are required. A number (either a floating point or an integer value) and the decimals to round the number to are the two parameters that the function takes. However, the second parameter is optional.

After the decimal point, if the last digit is greater than 5, it will round the value to the next whole/integer number; if it is less than 5, it will round to the floor integer.

If only the first parameter is specified, an integer value will be returned as output.

However, the round() function will return a float 1.0 if the second parameter is passed as zero:

The second parameter i.e. ndigits is used to specify the precision value for the round-off number which is obtained using the round() function.

The supplied numbers are rounded to two decimal places as the second parameter is specified as 2. We can specify a value for ndigits (the second parameter) according to our requirements. If you want to round that many digits before the decimal point, you may also give the round() function a negative value as the second argument. If -1 will be specified as the second argument the supplied number will be rounded to its nearest 10. If -2 is specified, the number will be rounded to the nearest 100, to the nearest 1000 when -3 is specified, and so on.

Rounding Numbers by Using the Truncate Concept

Truncating a number to a specific number of digits is the easiest, albeit crudest, way to round a number. When a number is truncated, the digits after a specific position are replaced with 0. For example, the value 11.480 will be truncated to tens place and will return 10, if truncated to one’s place it returns 11, if 11.480 is truncated to tenths place will return 11.4 will be returned, if truncated to the hundredths place, it will return 11.48, and so on. Python’s truncate() method allows for the usage of both negative and positive values.

The truncate method can be implemented using the following technique:

  • The integer will be multiplied by 10p to move the decimal point p places to the right.
  • Using int() to obtain the integer portion/part of the new number.
  • By dividing by 10p, the decimal place is moved left by p places.

Rounding Numbers by ceil() and floor() Functions

This ceil () and floor() functions are provided in the math module.

ceil(): It takes a decimal number as a parameter and returns an integer that is equal to or larger than the input number.

floor(): It takes a decimal number as a parameter and returns an integer that is equal to or less than the input number.

Rounding Up Number Using ceil() Function

Rounding up means rounding a float/number to the nearest integer value which is greater than the supplied float/number. A value/number is rounded up to a certain number of digits in the rounding-up process. The following is one method to apply the rounding-up function:

  • First, by multiplication of n by 10 ** decimals, the decimal point in n will be moved to the specified number of locations to the right.
  • The Math.ceil() function rounds the new value to the nearest number/integer.
  • Finally, divide by 10 ** decimals to move the decimal point back to the left.

Rounding Down Number Using floor() Function

Rounding down means rounding a float/number to the nearest integer value which is smaller or lower than the supplied float/number. An integer/float is rounded down in rounding down to a predetermined number of digits. The following is one method to apply the rounding-up function:

  • First, by multiplication of n by 10 ** decimals, the decimal point in n is moved to the specified number of locations/places to the right.
  • The new number will be rounded to the nearest integer using math.floor() function.
  • Finally, divide by 10 ** decimals to move the decimal point back to the left.

Rounding Numbers Using the Rounding Bias Concept

Using the concept of symmetry, the rounding bias concept is introduced and it defines how rounding impacts numerical values in a dataset. The rounding-up approach has a bias in favor of positive infinity because the number is always rounded up toward positive infinity. Similar biases exist in the rounding-down approach, which favors negative infinity. For positive numbers, the truncate approach/technique has a bias toward negative infinity, and for negative numbers, it has a bias toward positive infinity. This type of behavior is known as a bias towards zero in rounding functions.

Rounding Half Up

The “rounding half up” strategy breaks ties via rounding up and rounds each float or integer to the closest number with the desired precision. To use the rounding-half-up technique, the decimal point of a number is shifted to the right by the specified number of places. In this instance, we must decide if the digit following the moved decimal point is larger than or less than 5. We can use math.floor() function to round the result down after adding 0.5 to the shifted value.

Rounding Half Down

This approach breaks ties by rounding the number to the smaller of the two values, unlike the rounding half-up strategy, which rounds to the closest number. The roundHalfUp() function’s math.floor() is swapped out for math.ceil() to apply the rounding half down technique and then 0.5 is subtracted rather than added.


In this article, we first saw an introduction to rounding numbers. Then we explain what floating numbers are and how we can round inters/floats in python. First, we used the round() function which is an inbuilt function in python to round values. We also implemented different functions like ceil() and floor() and techniques/concepts like truncation and round bias for rounding the numbers.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.