-
PYTHON > Maths et calculs
- The number
n
rounded tondigits
.
OPÉRATIONS
puissance
x**n # x à la puissance n
NOMBRE AU HASARD
randint(a,b) # renvoie un entier entre a et b compris.
random() # renvoie un flottant au hasard dans l'intervalle [0 ; 1[
uniform(a,b) #renvoie un flottant au hasard entre a et b
0.8129559098175104
12.93990221807783
arrondir un nombre décimal
round(3.1415) 3
round(1.4567,1) 1.5 round(1.4567,2) 1.46
Convertir un nombre réel en entier
int(3.1415) 3
Arrondir une matrice de nombres réels
import numpy as np a = np.array(([1.24,3.46,5.34])) a array([1.24, 3.46, 5.34]) np.around(a, decimals=1) array([1.2, 3.5, 5.3])
>>> import numpy as np >>> a = np.array(([1,24,3.46,5.34])) >>> a array([ 1. , 24. , 3.46, 5.34]) >>> a.astype(int) array([ 1, 24, 3, 5])
Utiliser format pour incorporer un nombre dans une chaîne de caractères
Si l’objectif est d’arrondir un nombre réel pour ensuite l’insérer dans une chaîne de caractères, le plus simple est d’utiliser format()
'Pi value is {:06.2f}'.format(3.141592653589793) 'Pi value is 003.14' 'Pi value is {:01.2f}'.format(3.141592653589793) 'Pi value is 3.14'
round()
round()
takes two numeric parameters,n
umber andndigits
. It returns the number rounded tondigits
. By default, the value of thendigits
parameter is zero.round(number, ndigits)
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:
print(round(12)) -> 12 print(round(12.7)) -> 13 print(round(12.4)) -> 12
Different ways to round down a number in Python:
1) Truncat
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 math.trunc(x)
x -> The decimal number that needs to be truncated.
12import
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) 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 math.floor(x)
x -> The decimal number that needs to be rounded down.
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) int()
int(value, base)
value: number or string to be converted to int
base: number format
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) numpy.floor()
numpy.floor(a)
a: input array
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) // 1
// 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
One can also round down a number using math.ceil() method.
—
- The number