Chloe Zhou
0
Q:

arrondire au centième python

#Arrondir un nombre réel
>>> round(3.1415)
3

#Arrondir un nombre réel en gardant 1 décimale
>>> x = 1.4567
>>> round(x,1)
1.5

#Convertir un nombre réel en entier
>>> x = 3.1415
>>> x = int(x)
>>> x
3
>>> type(x)
<class 'int'>

#Note: donne la même chose que la fonction round()
>>> x = round(3.1415)
>>> type(x)
<class 'int'>
>>> x
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])

#Pour convertir une matrice de nombres réels en nombre entier il y a la fonction astype:
>>> 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])

#Arrondir un nombre complexe
>>> z = 2.14 + 3.47j
>>> round(z.real, 1) + round(z.imag, 1) * 1j
(2.1+3.5j)

#Utiliser format pour incorporer un nombre dans une chaîne de caractères
>>> s = 'Pi value is {:06.2f}'.format(3.141592653589793)
>>> s
'Pi value is 003.14'
>>> s = 'Pi value is {:01.2f}'.format(3.141592653589793)
>>> s
'Pi value is 3.14'
1

New to Communities?

Join the community