How to Round in Python

How to Round in Python

In this era, every business often works with big data, and they try to use their data to make informed decisions. Since Python is a powerful general-purpose programming language, many firms have begun to use Python's powerful Data Science Functions. Rounding is one of the functions that play a significant role when you are manipulating numeric datasets. So, it is essential to know how to round in Python.

There are various methods to round in Python. Let's look at them one by one.

Method 1: Using Built-in round() function

Python has a built-in function called "round()", which takes two parameters, number, and ndigits. This function rounds the given number to a given precision in decimal digits.

Syntax:

  • round(number, ndigits)

Parameters:

  • number – the number to be rounded
  • ndigits – the number of digits up to which the number is to be rounded

The ndigits parameter is optional, and it defaults to zero.

first method of how to round in Python

Method 2: Using math.ceil() and math.floor() functions

First, you need to import the "math" library to use these two functions.

1. math.ceil() function

This function rounds up the given number and returns the nearest whole number greater than or equal to the given number.

second method on how to round in python

2. math.floor() function

This function rounds down the given number and returns the nearest whole number less than or equal to the given number.

this image is about the second method

Method 3: Using the Truncation concept

Truncation is one of the simplest methods to round a number in Python. You can apply this concept in two ways.

1. math.trunc() function

This function truncates the number to the nearest whole number towards zero. First, you need to import the "math" library to use this function.

this image is about the method 3 on how to round in python

2. truncate() function

It is a user-defined function. This function truncates the number to a given number of digits. It converts each digit to zero after the given position.

First, you need to define the function, and then you can use it anywhere.

this image shows the function for truncation

The explanation for the truncate() function code:

  • First, multiply the number by 10decimals to shift the decimal point to the given number of digits to the right.
  • Then take the whole part of the new number using the int() function.
  • Finally, divide the integer number by 10decimals to shift the decimal point back to the left.

Syntax:

  • truncate(n, decimals)

Parameters:

  • n – the number to be truncated
  • decimals – the number of digits up to which the number is to be truncated

The "decimals" parameter is optional, and it defaults to zero.

truncate function using positive numbers

You can also apply this function to negative numbers.

truncate function using negative numbers

Method 4: Using the Rounding Up concept

This concept helps to round up the given number to a given number of digits.

First, you need to create a user-defined function, which you can then use to round up the numbers.

image shows the round up function code on how to round in python

The explanation for the round__up() function code:

  • First, multiply the number by 10decimals to shift the decimal point to the given number of digits to the right.
  • Then round up the new value using math.ceil() function.
  • Finally, divide the integer number by 10decimals to shift the decimal point to the given number of digits to the left.

Syntax:

  • round__up(n, decimals)

Parameters:

  • n – the number to be rounded up
  • decimals – the number of digits up to which the number is to be rounded up
example for round up function

Method 5: Using the Rounding Down concept

This concept helps to round down the given number to a given number of digits.

First, you need to create a user-defined function, which you can then use to round down the numbers.

this image is about the method 5 on how to round in python

The explanation for the round__down() function code:

  • First, multiply the number by 10decimals to shift the decimal point to the given number of digits to the right.
  • Then round down the new value using math.floor() function.
  • Finally, divide the integer number by 10decimals to shift the decimal point to the given number of digits to the left.

Syntax:

  • round__down(n, decimals)

Parameters:

  • n – the number to be rounded down
  • decimals – the number of digits up to which the number is to be rounded down
example for method 5

Method 6: Rounding Half Up Concept

This concept breaks ties by rounding up the given number to the nearest whole number with particular accuracy.

this image is about the method 6 on how to round in python

The explanation for the round__half__up() function code:

  • First, multiply the number by 10decimals to shift the decimal point to the given number of digits to the right.
  • After that, add 0.5 to the new value.
  • Finally, round down the value using math.floor() function.

Syntax:

  • round__half__up(n, decimals)

Parameters:

  • n – the number to be rounded half up
  • decimals – the number of digits up to which the number is to be rounded half up
example for method 6

Method 7: Rounding Half Down Concept

This concept breaks ties by rounding down the given number to the nearest whole number with particular accuracy.

this image is about the method 7 on how to round in python

The explanation for the round__half__down() function code:

  • First, multiply the number by 10decimals to shift the decimal point to the given number of digits to the right.
  • After that, subtract 0.5 from the new value.
  • Finally, round up the value using math.ceil() function.

Syntax:

  • round__half__down(n, decimals)

Parameters:

  • n – the number to be rounded half up
  • decimals – the number of digits up to which the number is to be rounded half up
example for method 7

Method 8: Rounding Half Away From Zero Concept

This concept is similar to the methods mentioned above.

As usual, first, shift the decimal point to the right of a given number of places.

Then you should consider the digit immediately available to the right of the decimal place in the new value and select one of the options below.

  • If the given number is positive and
    • digit >= 5, then Round Up
    • digit = 5, then Round Down
  • If the given number is negative and
    • digit >= 5, then Round Down
    • digit < 5, then Round Up

After finished rounding, you can shift the decimal point back to the left.

Method 9: Rounding Half To Even Concept

This concept is to round the number to the nearest even number at the desired precision. This concept uses Python's built-in round() function.

To use this concept, first, you need to import the "Decimal" function from the "decimal" library.

In this method, rounding is done with the ".quantize()" function. This function determines the number of decimal places to round the number.

this image is about the method 9 on how to round in python

If you are new to Python, visit the previous guide to learn how to run a Python script to get the fundamental knowledge.

I hope you all have understood how to round in Python. I'll meet you all soon with more exciting content.

Kathurshana Sivalingam

Author of Get Basic Idea / Undergraduate at University of Moratuwa.

Leave a Reply

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