Problème pour compter le nombre de caractère d'une variable numérique

Résolu
Thomas95390 Messages postés 2 Date d'inscription vendredi 2 octobre 2009 Statut Membre Dernière intervention 3 octobre 2009 - 2 oct. 2009 à 17:04
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 - 3 oct. 2009 à 14:07
Bonjour,

Voilà je suis débutant en Visual basic et donc je suis entrain de faire ... une calculatrice (pas vraiment révolutionnaire). Mon problème correspond à l'affichage du nombre dans la textbox résultat.
Pour pouvoir obtenir un nombre décimal, j'ai besoin de déterminer le nombre de caractère dans la variable Nombre_1_décimal pour obtenir la partie décimal du nombre que j'ajouterais à la partie entière. J'ai utilisé la fonction Len(), mais au premier chiffre décimal que j'écrit j'obtiens la valeur 8 alors que j'attends 1.

Voici un bout du programme qui vous aidera à comprendre ma démarche :


Private Sub CmdChiffre_Click(Index As Integer)
If Virgule = False Then
If CmdChiffre(Index).Index 0 Then Nombre_1 (Nombre_1 * 10)
If CmdChiffre(Index).Index 1 Then Nombre_1 (Nombre_1 * 10) + 1
If CmdChiffre(Index).Index 2 Then Nombre_1 (Nombre_1 * 10) + 2
If CmdChiffre(Index).Index 3 Then Nombre_1 (Nombre_1 * 10) + 3
If CmdChiffre(Index).Index 4 Then Nombre_1 (Nombre_1 * 10) + 4
If CmdChiffre(Index).Index 5 Then Nombre_1 (Nombre_1 * 10) + 5
If CmdChiffre(Index).Index 6 Then Nombre_1 (Nombre_1 * 10) + 6
If CmdChiffre(Index).Index 7 Then Nombre_1 (Nombre_1 * 10) + 7
If CmdChiffre(Index).Index 8 Then Nombre_1 (Nombre_1 * 10) + 8
If CmdChiffre(Index).Index 9 Then Nombre_1 (Nombre_1 * 10) + 9
End If

If Virgule = True Then
If CmdChiffre(Index).Index 0 Then Nombre_1_décimal (Nombre_1_décimal * 10)
If CmdChiffre(Index).Index 1 Then Nombre_1_décimal (Nombre_1_décimal * 10) + 1
If CmdChiffre(Index).Index 2 Then Nombre_1_décimal (Nombre_1_décimal * 10) + 2
If CmdChiffre(Index).Index 3 Then Nombre_1_décimal (Nombre_1_décimal * 10) + 3
If CmdChiffre(Index).Index 4 Then Nombre_1_décimal (Nombre_1_décimal * 10) + 4
If CmdChiffre(Index).Index 5 Then Nombre_1_décimal (Nombre_1_décimal * 10) + 5
If CmdChiffre(Index).Index 6 Then Nombre_1_décimal (Nombre_1_décimal * 10) + 6
If CmdChiffre(Index).Index 7 Then Nombre_1_décimal (Nombre_1_décimal * 10) + 7
If CmdChiffre(Index).Index 8 Then Nombre_1_décimal (Nombre_1_décimal * 10) + 8
If CmdChiffre(Index).Index 9 Then Nombre_1_décimal (Nombre_1_décimal * 10) + 9
End If
'Nombre_1_décimal = Nombre_1_décimal * 10 ^ (-Len(Nombre_1_décimal))
TxtRésultat.Text = Nombre_1 + Nombre_1_décimal
End Sub

Quelqu'un serait assez aimable pour m'expliquer la bonne démarche.

Merci beaucoup

3 réponses

PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
2 oct. 2009 à 18:24
salut,
Nombre_1_décimal est un numérique apparemment
len travaille sur une chaine

Len(CSTR(Nombre_1_décimal)) te renverra la taille réelle :
8 => 1
0 => 1
44 => 2


cependant ton code est du grand n'importe quoi....
regarde les redondances ?!!
if virgule then
  Nombre_1_décimal = (Nombre_1_décimal * 10) + index
else
  Nombre_1 = (Nombre_1 * 10) + index
endif


çà simplifie déjà, reste à voir le reste mais bon

NB : regarde les sources existantes, çà peut te donner un peu d'inspiration

++

[hr]
3
Thomas95390 Messages postés 2 Date d'inscription vendredi 2 octobre 2009 Statut Membre Dernière intervention 3 octobre 2009
3 oct. 2009 à 11:37
Merci pour ton aide. Mais comme je débute que depuis peut j'ai pas encore les automatisme pour voir les simplifications que je peux faire.
Enfin merci pour ton aide.
0
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
3 oct. 2009 à 14:07
pas de problème, çà viendra au fur et à mesure

ps : si la réponse te convient, pense à la valider par le bouton "réponse acceptée"

bonne continuation
0
Rejoignez-nous