Bonsoir,
sous Windows XP, Python 2.5, j'importe un fichier data.csv via csv reader.
Je veux convertir les string contenus dans le .csv en format numérique.
Par ex :
for i in range(1,nb_lg):
for j in range(0,nb_col):
item=data[i][j]
if item is not '':
item=item.replace(',','.')
data[i][j]=string.atof(item)
Mais pour certaines valeurs, par ex. item='0,95', ça me le transforme en item=0.95000000001 ou item=0.94999999999 !
Manque de Précision ?
Comment toujours obtenir item=0.95 ?
Message édité par masklinn le 23-03-2009 à 21:35:54
---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?
---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?
---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?
gennosuke
Posté le 23-03-2009 à 22:43:43
OK, c'est bon.
Après un temps de réflexion supérieur à 3s, j'ai remplacé atof par Decimal.
On trimballe des Decimal(".." ) partout, mais la variable est changée.
Merci
Taz
bisounours-codeur
Posté le 24-03-2009 à 06:46:18
Je vous aime bien avec le Decimal, mais on dirait un cours de maths de première année de fac: la majorité des étudiants est incapable d'admettre que 0.99999... = 1. C'est à dire qu'un nombre n'a pas une représentation unique. (Et vice versa en float .
Au final, sauf calcul financier, rester en float convient très bien. Si les ....02 dérange quelqu'un, y a qu'à formater la sortie et on en parle plus.