Problème d'addition 2

serhoe - 17 juin 2013 à 15:29
 Utilisateur anonyme - 17 juin 2013 à 19:54
Salut tout le monde,

J'ai un tableau qui contient des valeurs que j'additionne et soustrait.
Malheureusement des fois j'ai :

71.99999 au lieu de 72
ou
495,610000000001 au lieu de 495,61

Mon code :

Const ForReading 1, ForWriting 2 
Dim oFso, f
Set oFso = CreateObject("Scripting.FileSystemObject")
Set f = oFso.OpenTextFile("C:\Users\Monique\Downloads\Fichier à importer (1).csv", ForReading)
ln=-1
cl=0
 
while Not f.AtEndOfStream '1ère itération pour définir les limites du tableau
  ln=ln+1 'définition indice lignes
  Tab=Split(f.ReadLine,";")
  If cl < UBound(Tab) Then cl = UBound(Tab) 
Wend
f.Close
'MsgBox "indice lignes : " & ln+1 & "indice colonnes :" & cl+1
 
Dim Tab2()
ReDim Tab2(ln,cl)
Set f = oFso.OpenTextFile("C:\Users\Monique\Downloads\Fichier à importer (1).csv", ForReading)
i=0
while Not f.AtEndOfStream ' 2ème itération pour remplir le tableau Tab2
  Tab = Split(f.ReadLine,";")
  For j = 0 to UBound(Tab)
    Tab2(i,j) = Tab(j)
  Next
  i=i+1
Wend
f.Close

Dim Tab3()
ReDim Tab3(ln-1,cl)

For i=1 to UBound(Tab2,1) ' copie tout de tab2 à tab 3 sauf 1ere ligne marche
For j=0 to Ubound(Tab2,2)'oui		
Tab3(i-1,j)=Tab2(i,j)'oui
Next 
Next

For i=0 to UBound(Tab3,1) ' on copie dans tab3 les clés dans la première colonne de chaque ligne
For j=0 to Ubound(Tab3,2)
Tab3(i,0)=Tab3(i,3)+Tab3(i,5)+Tab3(i,6)+Tab3(i,9)
Next
Next 

For i=0 to Ubound(Tab3,1) ' on décale tous les mois vers la gauche
For j=1 to Ubound(Tab3,2)-20
Tab3(i,j)=Tab3(i,j+20)
Next
Next

Dim fsot, ft   
Set fsot = CreateObject("Scripting.FileSystemObject")
Set ft = fsot.OpenTextFile("C:\Users\Monique\Downloads\lololololololo.txt", 2,true)

dim d,i,t, Tab4
t = Tab3
Set d = CreateObject("Scripting.Dictionary") 
For I = 0 To UBound(Tab3)
  'a d.Keys : b d.items
  if not d.Exists(t(i,0)) then
    d.add Tab3(i,0),i
    t(i,1) = Tab3(i,1)
    t(i,2) = Tab3(i,2)
t(i,3) = Tab3(i,3)
    t(i,4) = Tab3(i,4)
t(i,5) = Tab3(i,5)
    t(i,6) = Tab3(i,6)
t(i,7) = Tab3(i,7)
    t(i,8) = Tab3(i,8)
t(i,9) = Tab3(i,9)
    t(i,10) = Tab3(i,10)
t(i,11) = Tab3(i,11)
    t(i,12) = Tab3(i,12)
   else
    ou = d(t(i,0))
    t(ou,1) = cdbl(Tab3(ou,1)) + cdbl(Tab3(i,1))
    t(ou,2) = cdbl(Tab3(ou,2)) + cdbl(Tab3(i,2))
t(ou,3) = cdbl(Tab3(ou,3)) + cdbl(Tab3(i,3))
t(ou,4) = cdbl(Tab3(ou,4)) + cdbl(Tab3(i,4))
t(ou,5) = cdbl(Tab3(ou,5)) + cdbl(Tab3(i,5))
t(ou,6) = cdbl(Tab3(ou,6)) + cdbl(Tab3(i,6))
t(ou,7) = cdbl(Tab3(ou,7)) + cdbl(Tab3(i,7))
t(ou,8) = cdbl(Tab3(ou,8)) + cdbl(Tab3(i,8))
t(ou,9) = cdbl(Tab3(ou,9)) + cdbl(Tab3(i,9))
t(ou,10) = cdbl(Tab3(ou,10)) + cdbl(Tab3(i,10))
t(ou,11) = cdbl(Tab3(ou,11)) + cdbl(Tab3(i,11))
t(ou,12) = cdbl(Tab3(ou,12)) + cdbl(Tab3(i,12))
    t(i,0) = ""
  end if
next
redim Tab4(d.count-1,13)
n = 0
for i = 0 to ubound(t)
  if t(i,0) <> "" then
    Tab4(n,0) t(i,0) :  Tab4(n,1) t(i,1) :  Tab4(n,2) = t(i,2) : Tab4(n,3) = t(i,3) : Tab4(n,4) = t(i,4) : Tab4(n,5) = t(i,5) : Tab4(n,6) = t(i,6) : Tab4(n,7) = t(i,7) : Tab4(n,8) = t(i,8) : Tab4(n,9) = t(i,9) : Tab4(n,10) = t(i,10) : Tab4(n,11) = t(i,11) : Tab4(n,12) = t(i,12)
    n = n + 1
  end if
next
For i=0 to UBound(Tab4,1) ' copie de tab3 dans le fichier et marche
For j=0 to UBound(Tab4,2)
If m < UBound(Tab4,2) Then 
ft.write(Tab4(i,j) & "	")
m=m+1
Else
ft.write(Tab4(i,j) & vbcrlf)
m=0
End If
Next
Next


Le contenu de Tab3 ressemble à ça :

2COMCT625712NESTLEPZZZZZ 0 0 -135,77 0 0 0 0 0 0 0 0 0
2COMCT622611CZZZZZPZZZZZ 0 -9500 -47250 0 0 0 0 0 0 0 0 0
2COMCT622611EMEAPZZZZZ 0 -15000 -750 -15000 0 0 0 0 0 0 0 0
2COMCT622614CZZZZZPZZZZZ -19500 -8250 91550 0 0 0 0 0 0 0 0 0
2COMCT623410CZZZZZPZZZZZ -16,8 0 0 0 0 0 0 0 0 0 0 0


La seule différence c'est que des clés (première colonne) peuvent être en plusieurs exemplaires, ce qui n'est plus le cas dans Tab4. Et donc le but et là où est mon problème : quand j'additionne deux valeurs qui sont dans la même colonnes d'une clé en double ex :

cle1 2 3
cle1 -2 8

je veux :

cle1 0 11

Mais des fois j'ai des problèmes donc de nombres (ils sont corrects mais des fois ça bug un peu 72.0999 au lieu de 72.1 ou 12.000001 au lieu de 12)

Merci d'avance de votre aide :)
(dès que ton code marchera ucfoutu je l'utiliserai )

3 réponses

ucfoutu Messages postés 18038 Date d'inscription lundi 7 décembre 2009 Statut Modérateur Dernière intervention 11 avril 2018 211
17 juin 2013 à 16:13
utilise csng, dans ce cas, plutôt que cdbl
regarde ===>>
toto = "71,099999"
msgbox csng(toto) 
toto = "495,610000000001"
msgbox csng(toto) 

dès que ton code marchera ucfoutu je l'utiliserai

Mais ... il marche maintenant (as-tu regardé ?) y compris pour des plus que doublons
PS : tu devrais réellement, par ailleurs, ouvrir une autre discussion à propos de ton tyab36 à partir de ton csv !
Tu y gagnerais beaucoup en agilité, mais également en clarté, simplicité et en vitesse d'exécution !
________________________
Réponse exacte ? => "REPONSE ACCEPTEE" facilitera les recherches.
Pas d'aide en ligne installée ? => ne comptez pas sur moi pour simplement répéter son contenu. Je n'interviendrai que si nécessité de la compléter.
0
Le code qui fonctionne c'est celui là :

Const ForReading 1, ForWriting 2 
Dim oFso, f
Set oFso = CreateObject("Scripting.FileSystemObject")
Set f = oFso.OpenTextFile("C:\Users\Monique\Downloads\Fichier à importer (1).csv", ForReading)
ln=-1
cl=0
 
while Not f.AtEndOfStream '1ère itération pour définir les limites du tableau
  ln=ln+1 'définition indice lignes
  Tab=Split(f.ReadLine,";")
  If cl < UBound(Tab) Then cl = UBound(Tab) 
Wend
f.Close
'MsgBox "indice lignes : " & ln+1 & "indice colonnes :" & cl+1
 
Dim Tab2()
ReDim Tab2(ln,cl)
Set f = oFso.OpenTextFile("C:\Users\Monique\Downloads\Fichier à importer (1).csv", ForReading)
i=0
while Not f.AtEndOfStream ' 2ème itération pour remplir le tableau Tab2
  Tab = Split(f.ReadLine,";")
  For j = 0 to UBound(Tab)
    Tab2(i,j) = Tab(j)
  Next
  i=i+1
Wend
f.Close

Dim Tab3()
ReDim Tab3(ln-1,cl)

For i=1 to UBound(Tab2,1) ' copie tout de tab2 à tab 3 sauf 1ere ligne marche
For j=0 to Ubound(Tab2,2)'oui		
Tab3(i-1,j)=Tab2(i,j)'oui
Next 
Next

For i=0 to UBound(Tab3,1) ' on copie dans tab3 les clés dans la première colonne de chaque ligne
For j=0 to Ubound(Tab3,2)
Tab3(i,0)=Tab3(i,3)+Tab3(i,5)+Tab3(i,6)+Tab3(i,9)
Next
Next 

For i=0 to Ubound(Tab3,1) ' on décale tous les mois vers la gauche
For j=1 to Ubound(Tab3,2)-20
Tab3(i,j)=Tab3(i,j+20)
Next
Next

Dim fsot, ft   
Set fsot = CreateObject("Scripting.FileSystemObject")
Set ft = fsot.OpenTextFile("C:\Users\Monique\Downloads\lololololololo.txt", 2,true)

Set objKeysDictionnary = CreateObject("Scripting.Dictionary") 'Création du dictionnaire contenant les clés
objKeysDictionnary.CompareMode = 1 'Mode de comparaison : texte
For i=0 to Ubound(Tab3,1) ' Pour chaque occurence du tableau Tab3
If objKeysDictionnary.Exists(Tab3(i,0)) Then
objKeysDictionnary.Item(Tab3(i,0)) = objKeysDictionnary.Item(Tab3(i,0)) + Tab3(i,1) 'Si la clé a déjà été trouvée, ajout des valeurs
Else
objKeysDictionnary.Add Tab3(i,0), Tab3(i,1) 'Sinon ajout de la clé au dictionnaire
End If
Next

i=0

For Each strKey In objKeysDictionnary.Keys 'Pour chaque clé contenue dans le dictionnaire
i=i+1
Next

Dim Tab4()
ReDim Tab4(i-1,13)

i=0

For Each strKey In objKeysDictionnary.Keys 'Pour chaque clé contenue dans le dictionnaire
Tab4(i,0)=strKey
i=i+1
Next

For i=0 to Ubound(Tab3,1)
For j=0 to Ubound(Tab4,1)
If Tab4(j,0)=Tab3(i,0) Then
Tab4(j,1)=cdbl(Tab4(j,1))+cdbl(Tab3(i,1))
Tab4(j,2)=cdbl(Tab4(j,2))+cdbl(Tab3(i,2))
Tab4(j,3)=cdbl(Tab4(j,3))+cdbl(Tab3(i,3))
Tab4(j,4)=cdbl(Tab4(j,4))+cdbl(Tab3(i,4))
Tab4(j,5)=cdbl(Tab4(j,5))+cdbl(Tab3(i,5))
Tab4(j,6)=cdbl(Tab4(j,6))+cdbl(Tab3(i,6))
Tab4(j,7)=cdbl(Tab4(j,7))+cdbl(Tab3(i,7))
Tab4(j,8)=cdbl(Tab4(j,8))+cdbl(Tab3(i,8))
Tab4(j,9)=cdbl(Tab4(j,9))+cdbl(Tab3(i,9))
Tab4(j,10)=cdbl(Tab4(j,10))+cdbl(Tab3(i,10))
Tab4(j,11)=cdbl(Tab4(j,11))+cdbl(Tab3(i,11))
Tab4(j,12)=cdbl(Tab4(j,12))+cdbl(Tab3(i,12))
End If
Next
Next	

m=0
Redim Preserve Tab4(Ubound(Tab4,1),13) ' pour le bon nombre de colonnes
For i=0 to UBound(Tab4,1) ' copie de tab3 dans le fichier et marche
For j=0 to UBound(Tab4,2)
If m < UBound(Tab4,2) Then 
if j<>0 Then
ft.write(round(Tab4(i,j),2) & "	")
m=m+1
else
ft.write(Tab4(i,j) & "	")
m=m+1
end if
Else
if j<>0 Then
ft.write(round(Tab4(i,j),2) & vbcrlf)
m=0
else
ft.write(Tab4(i,j) & vbcrlf)
m=0
end if
End If
Next
Next


Là dedans j'ai tout qui marche même les arrondissement (voir à la fin).
Tu veux dire que je dois faire ça :

Tab4(j,12)=csng(Tab4(j,12))+csng(Tab3(i,12))
0
Utilisateur anonyme
17 juin 2013 à 19:54
Bonjour,

J'ai un tableau qui contient des valeurs que j'additionne et soustrait.
Malheureusement des fois j'ai :

71.99999 au lieu de 72
ou
495,610000000001 au lieu de 495,61



C'est juste normal. Cela vient de la conversion du décimal au binaire et du binaire au décimal. Tous les ordinateurs, tous les langages et tous les programmes sont sujets à ces choses-là. Cela ne paraît pas dans Excel, parce que l'affichage à 15 chiffres est arrondi ou tronqué; mais c'est là quand même. Malheureusement VBScript n'est pas équipé pour veiller tard avec les chiffres et n'a pas d'arrondi automatique.
0
Rejoignez-nous