Tuesday, February 9, 2021

Python Pool: Best Ways to Round Down Numbers in Python

Hello coders!! In this article, we will be learning the different ways to round down a number in Python. The rounding of a number is basically making the number simpler while keeping its value close to what it was. Let us see how we can round down a number in Python.

Inbuilt round() function in Python:

In Python, we are provided with a built-in round() function. This function takes two numeric parameters, number and ndigits. It returns the number rounded to ndigits. By default, the value of the ndigits parameter is zero.

Syntax:

round(number, ndigits)

Parameters:

  • number – the number which needs to be rounded
  • ndigits (optional) – The value up to which the given number needs to be rounded. Its default value is 0.

Return Value:

  •  The number n rounded to ndigits.
print(round(12))
print(round(12.7))  
print(round(12.4))

OUTPUT:

12
13
12

We have not passed any value to the ndigits parameter in this example. As a result, it takes the default value, i.e., 0. We can see that the integer number remains at it is, 12.7 is rounded up to 13, and 12.4 is rounded down to 12.

Understanding the problem with round() method:

print(round(2.5))  
print(round(3.5))

OUTPUT:

2
4

As we can see that 2.5 is rounded down to 2, whereas 3.5 is rounded up to 4. Well, this is no glitch in Python programming. It’s just how the round() function works. Let us explore some different ways to round down an integer in Python.

Different ways to round down a number in Python:

We will discuss 5 other methods to round down a number in python except the round() method.

1) Truncation Method to Round down in Python:

As the name suggests, truncation is used to shorten things. It is a straightforward method that can be used to round a number by truncating a given number of digits.

Value Truncated To Result
12.345 Tens place 10
12.345 Ones place 12
12.345 Tenths place 12.3
12.345 Hundredths place 12.34

Syntax:

math.trunc(x)

Parameter:

  • x -> The decimal number that needs to be truncated.

Return Value:

  • Truncated integer part of a number

Example:

import math
print( math.trunc(3.5) )
3

In this example, we have used inbuilt math.trunc() method of Python from the math module to obtain the integer part of the given decimal number.

2) Using math.floor():

A number can be rounded up to a certain digit.

Value Rounded Down To Result
19.345 Tens place 10
19.345 Ones place 19
19.345 Tenths place 19.3
19.345 Hundredths place 19.34

Syntax:

math.floor(x)

Parameter:

  • x -> The decimal number that needs to be rounded down.

Return Value:

  • The number rounded down to nearest integer.

Example:

import math
print( math.floor(3.5) )
3

As we can see in this example, the number is rounded up to the nearest integer greater than the number itself.

3) Using int() to round down a number in python:

This method is essentially used to convert the given number into integer.

Syntax:

int(value, base)

Paramters:

  • value: number or string to be converted to int
  • base: number format

Example:

x = 1.3131
x = int(x)
print(x)
1

Here, we used the int() method to convert the decimal number into an integer, thus rounding it down to the nearest possible integer value.

4) Using numpy.floor() to round down:

It is a mathematical function that returns the floor of the elements of array. 

Syntax:

numpy.floor(a)

Parameter:

  • a: input array

Return Value:

  • array containing the floor value of every element

Example:

import numpy as np
x = np.array([1.22, 1.67, 2.53])
x = np.floor(x)
print(x)
[1. 1. 2.]

As you can see, using the numpy.floor() method, we were able to round down the elements of a given array.

5) Using // operator to round down in Python:

// is the floor division operator of Python.  It returns floor value for both integer and floating-point arguments. 
 

x = 1.3131
x = x // 1
print(x)

Output:

-1

Conclusion:

With this, we come to an end with this article. These are the two ways the number can be rounded down in Python. One can also round down a number using math.ceil() method.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

The post Best Ways to Round Down Numbers in Python appeared first on Python Pool.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...