IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Convertir pouces décimaux en pouces fractionaires ??


Sujet :

VB.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Mars 2010
    Messages
    85
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2010
    Messages : 85
    Par défaut Convertir pouces décimaux en pouces fractionaires ??
    Bonjour a tous !! Suite à cette discussion, j'ai tout refait mon programme avec VS 2015 et là...je doit faire exactement l'inverse de ce que je demandais à la base !!!

    Donc je suis en VB.NET et je dois convertir automatiquement (en appuyant sur TAB ou avec la méthode Key_Press) convertir une donnée en pouces décimal en pouces Fractionaire ex: Si je tape 24.875, quand je changerai de TextBox, ce sera écrit 24 7/8 ?!?

    J'ai essayer plusieurs méthodes dont celle d'Euclide mais le code ne fonctionne qu'avec les chiffres après la virgule !!
    J'ai donc ceci mais j'ai une erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    Public Class Form1
     
     
        Function GetFraction(ByVal d As Double) As String
            ' Get the initial denominator: 1 * (10 ^ decimal portion length)
            Dim Denom As Int32 = CInt(1 * (10 ^ d.ToString.Split("."c)(1).Length))   'Erreur ici si je met 0 dans le textbox1 !!
     
            ' Get the initial numerator: integer portion of the number
            Dim Numer As Int32 = CInt(d.ToString.Split("."c)(1))
     
            ' Use the Euclidean algorithm to find the gcd
            Dim a As Int32 = Numer
            Dim b As Int32 = Denom
            Dim t As Int32 = 0 ' t is a value holder
     
            ' Euclidean algorithm
            While b <> 0
                t = b
                b = a Mod b
                a = t
            End While
     
            'Get whole part of the number
            Dim Whole As String = d.ToString.Split("."c)(0)
     
            ' Return our answer
            Return Whole & " " & (Numer / a) & "/" & (Denom / a)
        End Function
     
     
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     
            Text2.Text = GetFraction(Text1.Text)  '<---  Jai une erreur ici : "La conversion de la chaîne "2.5" en type 'Double' n'est pas valide."
        End Sub
    End Class
    ce test est fait avec 2 TextBox et un Boutton mais je préférerais qu'il fonctionne au TAB ou au KeyPress mais j'ignore comment BIEN appeler la procédure !!
    et en plus de l'erreur de type Double...si je met 0 dans le textbox j'ai aussi une erreur !!
    Merci de m'aider !!!


  2. #2
    Inactif  

    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2012
    Messages
    4 903
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 69
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2012
    Messages : 4 903
    Billets dans le blog
    36
    Par défaut
    Bonjour,

    Je pense que la conversion vers double se fait en fonction des paramètres régionaux de la machine.

    Je n'ai jamais testé en VB.net, et là mon VS est planté, mais en VB6-VBA avec la virgule comme séparateur décima:

    Val(2,5) ---> plante
    CDbl(2,5) ---> OK.

    Mais, tu peux tricher vite fait, surtout pour juste tester au cas où tu aurais la virgule comme séparateur décimal. Puisque les américains sont à peu près les seuls à utiliser le point comme séparateur décimal, tu mets à quelque part, de préférence au lancement de l'application :

    My.application.changeculture("en-us")

    et Bingo, tu viens de virer boute pour boute ton séparateur décimal...

  3. #3
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Mars 2010
    Messages
    85
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2010
    Messages : 85
    Par défaut
    Merci pour ta réponse !! C'était effectivement un problème de séparateur !!!! mais j'ai comme un autre problème...Quand j'appuie sur le boutton la conversion se fait mais les chiffres qui sont apres la virgules sont aussi dans la réponse !!

    ex: si je met 2.125 dans le textbox1 et que je clique sur le bouton...le textbox2 m'indique 2.125 1/8 ???

    revoila mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    Public Class Form1
     
        Private Sub Text1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress
            If e.KeyChar = "."c Then e.KeyChar = ","c
        End Sub
     
        Function GetFraction(ByVal d As Double) As String
            ' Get the initial denominator: 1 * (10 ^ decimal portion length)
            Dim Denom As Int32 = CInt(1 * (10 ^ d.ToString.Split(","c)(1).Length))
     
            ' Get the initial numerator: integer portion of the number
            Dim Numer As Int32 = CInt(d.ToString.Split(","c)(1))
     
            ' Use the Euclidean algorithm to find the gcd
            Dim a As Int32 = Numer
            Dim b As Int32 = Denom
            Dim t As Int32 = 0 ' t is a value holder
     
            ' Euclidean algorithm
            While b <> 0
                t = b
                b = a Mod b
                a = t
            End While
     
            'Get whole part of the number
            Dim Whole As String = d.ToString.Split("."c)(0)
     
            ' Return our answer
            Return Whole & " " & (Numer / a) & "/" & (Denom / a)
        End Function
     
     
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     
     
            Text2.Text = GetFraction(Text1.Text)
     
        End Sub
    End Class

  4. #4
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Mars 2010
    Messages
    85
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2010
    Messages : 85
    Par défaut RÉSOLU
    Citation Envoyé par Biggy30 Voir le message
    Merci pour ta réponse !! C'était effectivement un problème de séparateur !!!! mais j'ai comme un autre problème...Quand j'appuie sur le boutton la conversion se fait mais les chiffres qui sont apres la virgules sont aussi dans la réponse !!

    ex: si je met 2.125 dans le textbox1 et que je clique sur le bouton...le textbox2 m'indique 2.125 1/8 ???

    revoila mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    Public Class Form1
     
        Private Sub Text1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress
            If e.KeyChar = "."c Then e.KeyChar = ","c
        End Sub
     
        Function GetFraction(ByVal d As Double) As String
            ' Get the initial denominator: 1 * (10 ^ decimal portion length)
            Dim Denom As Int32 = CInt(1 * (10 ^ d.ToString.Split(","c)(1).Length))
     
            ' Get the initial numerator: integer portion of the number
            Dim Numer As Int32 = CInt(d.ToString.Split(","c)(1))
     
            ' Use the Euclidean algorithm to find the gcd
            Dim a As Int32 = Numer
            Dim b As Int32 = Denom
            Dim t As Int32 = 0 ' t is a value holder
     
            ' Euclidean algorithm
            While b <> 0
                t = b
                b = a Mod b
                a = t
            End While
     
            'Get whole part of the number
            Dim Whole As String = d.ToString.Split("."c)(0)
     
            ' Return our answer
            Return Whole & " " & (Numer / a) & "/" & (Denom / a)
        End Function
     
     
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     
     
            Text2.Text = GetFraction(Text1.Text)
     
        End Sub
    End Class

    OK on oublie !!! Ca marche j'avais oublié de changer un point pour une virgule sur cette ligne : Dim Whole As String = d.ToString.Split("."c)(0)

    MERCI BEAUCOUP PROBLÈME RÉGLÉ !!!!!

  5. #5
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Mars 2010
    Messages
    85
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2010
    Messages : 85
    Par défaut Encore un petit problème...
    vous vous souvenez de mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
        Function GetFraction(ByVal d As Double) As String
            'TRANSFERE LES POUCES DÉCIMAL EN FRACTIONAL (Code pris sur le net ! )
     
            ' Get the initial denominator: 1 * (10 ^ decimal portion length)
            Dim Denom As Int32 = CInt(1 * (10 ^ d.ToString.Split(","c)(1).Length))
     
            ' Get the initial numerator: integer portion of the number
            Dim Numer As Int32 = CInt(d.ToString.Split(","c)(1))
     
            ' Use the Euclidean algorithm to find the gcd
            Dim a As Int32 = Numer
            Dim b As Int32 = Denom
            Dim t As Int32 = 0 ' t is a value holder
     
            ' Euclidean algorithm
            While b <> 0
                t = b
                b = a Mod b
                a = t
            End While
     
            'Get whole part of the number
            Dim Whole As String = d.ToString.Split(","c)(0)
     
            ' Return our answer
            Return Whole & " " & (Numer / a) & "/" & (Denom / a)
        End Function
    Bon ! Je croyais qu'il marchais nickel jusqu'a ce qu'il me lance une erreur si j'entre une valeurs entière....Impossible de convertir en Double...et avec raison car si je tappe 11.125 qui est un Double, la conversion s'effectue sans problème, mais si je tappe 20 là j'ai cette erreur car il n'y a pas de virgule, et même si je mets 20.0 ca ne marche pas....Pourtant dans le code il me semble y avoir une portion ou l'on mentionne les entiers :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
            'Get whole part of the number
            Dim Whole As String = d.ToString.Split(","c)(0)
     
            ' Return our answer
            Return Whole & " " & (Numer / a) & "/" & (Denom / a)
        End Function
    Mais je ne suis pas très famillier avec les code qui implique des maths !!!

    Si quelqu'un a une idée..Merci beaucoup !!!

  6. #6
    Membre Expert
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    700
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 700
    Par défaut
    Bonjour,
    la fonction peut retourner autant de valeur que de cas exposés (If... Else...).
    Avec votre code et quelques fonctions glanées, pour éviter les String et la Culture du PC, voici un bout de code à tester :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
        Function GetFraction1(ByVal d As Decimal) As String
     
            'Get whole part of the number
            Dim Whole As Decimal = Math.Truncate(d)
     
            'each case has its return
            If d - Whole = 0 Then
                Return Whole.ToString
            Else
                Dim DecimalNbr As Integer = NumberOfDecimals(d)
                Dim DecimalPart As Integer = CInt((d - Math.Truncate(d)) * Math.Pow(10, DecimalNbr))
     
     
     
                ' Use the Euclidean algorithm to find the gcd
                Dim a As Int32 = DecimalPart
                Dim b As Int32 = CInt(Math.Pow(10, DecimalNbr))
                Dim t As Int32 = 0
     
                ' Euclidean algorithm
                While b <> 0
                    t = b
                    b = a Mod b
                    a = t
                End While
     
                ' Return our answer
                Return (Whole & " " & (DecimalPart / a) & "/" & (CInt(Math.Pow(10, DecimalNbr)) / a)).ToString
            End If
     
        End Function
     
        Private Function Frac(d As Decimal) As Decimal
            d = Math.Abs(d)
            Return d - Math.Truncate(d)
        End Function
        Private Function NumberOfDecimals(x As Decimal) As Integer
            x = Math.Abs(x)
            Dim nPlaces = 0
     
            While Frac(x) > 0
                x = x * 10D
                nPlaces += 1
            End While
     
            Return nPlaces
     
        End Function

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2010] Copier/coller de données en pouces les convertir en cm automatiquement
    Par Legirondin33 dans le forum Excel
    Réponses: 1
    Dernier message: 01/04/2013, 21h01
  2. Convertir Fraction (pouces) en décimal(pouces)
    Par Biggy30 dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 10/03/2012, 16h02
  3. Requete sélection, Jointure (coup de pouce needed)
    Par Odilon dans le forum Langage SQL
    Réponses: 5
    Dernier message: 20/10/2005, 11h48
  4. Besoin d'1 coup de pouce pour realiser une importation de BD
    Par gizmorambo dans le forum Bases de données
    Réponses: 4
    Dernier message: 25/08/2005, 14h07
  5. besoin d'un ptit coup de pouce
    Par mickyoun dans le forum Langage SQL
    Réponses: 2
    Dernier message: 26/03/2004, 11h58

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo