How to round a number in Python

Rounding a number is a common operation we often need to carry to ensure that the value in slightly changed to ensure its readability. In this tutorial, we’ll explain how you can round a number in Python and explain some common use cases and how we can solve them.

Use the round() function to round a number

Python comes with a built-in function, round() that takes two parameters: a number and the number of digits we need after the decimal point. If no number of digits is specified, the function will round up and down to the closest integer.

print(round(3.2)) print(round(3.269,2))
Code language: Python (python)

Output:

3 3.27
Code language: Python (python)

The floating-point precision, represented by the second parameter in our function, will often be left empty or with a small number; otherwise you’ll end up removing the main benefits of this function. One surprising behavior from this function is that if we run round(3.675, 2), it will return 3.67 instead of the expected 3.68. This is not a bug and the reason is explained in the official documentation, but it is counterintuitive, and we must be aware of it.

Use math to round up or down

While this function comes handy, we can’t control whether we round up or down a number. In some cases, you’ll want to round down, even if the number is 3.7 for instance. To get this behavior, we should use the math library, coming with two handy methods:

  • math.floor() will return the largest integer less than or equal to x
  • math.ceil() will return the smallest integer greater than or equal to x

Both takes only one argument, and you can’t get a decimal value when you round up or down using this library.

import math print(math.floor(3.7)) print(math.ceil(3.7))
Code language: Python (python)

Output:

3 4
Code language: Python (python)

Round to the nearest tenth, hundreds or thousands

If we need to round to the nearest multiple of 10, 100 or even 1000 (most common cases), we can use the round() function or the math library with some tweaks.

Using round()

In our example at the beginning of this article, we explained the second parameters of the round() function is used to indicate the number of digits we want to keep. If you use a negative value, you’d actually let the function know that:

  • -1: you want to round to the closest 10
  • -2: you wish to round to the closest 100
  • -3: you wish to round to the closest 1000
  • etc…
print(round(987,-1)) print(round(1010,-2)) print(round(1987,-3))
Code language: Python (python)

Output:

990 1000 2000
Code language: Python (python)

Awesome, right? Now let’s see how we can achieve the same using the math library.

Using the math library

The logic is less straightforward (in my opinion) but can be achieved with a reduced number of lines. Let’s do it with 990. Two steps:

  • We first calculate the math.ceil() or math.floor(), divided by the multiple we want to get. Let’s assume I want a multiple of 100 here:
print(math.ceil(990/100)) #10
Code language: PHP (php)
  • We then multiply it by the multiple again to get the closest multiple of 100
print(math.ceil(990/100)*100) #1000
Code language: PHP (php)

The same logic can be applied with math.floor() obviously: it will depend on whether you want to round up or down.

Format a number

Sometimes, we wish to print a number without a decimal. Even if we don’t want to modify the value of the variable holding this number. To truncate a float in Python, you just need to use the format() method.

value = 132.8747474 print('the value is equal to {}'.format(round(value, 2)))
Code language: Python (python)

Output:

the value is equal to 132.87
Code language: Python (python)

Leave a Reply

Your email address will not be published. Required fields are marked *