Convertir un chiffre en lettre

Description

Se petit programme est réaliser sous "Microsoft Visual Studio 2010" est pour convertir un chiffre en toute lettre.

Source / Exemple :


Imports System.Globalization

Public Class Form1

    Private Property nonNumberEntered As Boolean

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim MySoundPlayer1 = New System.Media.SoundPlayer("C:\Program Files\Windows NT\Pinball\SOUND14.WAV")
        Dim MySoundPlayer2 = New System.Media.SoundPlayer("C:\Program Files\Windows NT\Pinball\SOUND16.WAV")
        
        If TextBox1.Text = "" And FR.Checked = False And AR.Checked = False Then
            MySoundPlayer2.Play()
            MsgBox("Entrer un Chiffre et Définir la Langue de convertion SVP!", MsgBoxStyle.Information, "Remarque")
        Else
            If TextBox1.Text <> "" And FR.Checked = False And AR.Checked = False Then
                MySoundPlayer2.Play()
                MsgBox("choisir une langue de convertion SVP!", MsgBoxStyle.Information, "Remarque")
                FR.Select()

            Else
                '============================= Français ==============================================
                If FR.Checked = True Then
                    If TextBox1.Text = "" Then
                        MySoundPlayer2.Play()
                        MsgBox("Entrer un Chiffre SVP!", MsgBoxStyle.Information, "Remarque")
                    Else
                        RichTextBox1.RightToLeft = Windows.Forms.RightToLeft.No
                        GroupBox2.RightToLeft = Windows.Forms.RightToLeft.No
                        GroupBox2.Text = "En Lettre"
                        RichTextBox1.Text = ConvNumberLetter(CDbl(TextBox1.Text), 0)
                        MySoundPlayer1.Play()
                        TextBox1.Text = Format(CDbl(TextBox1.Text), "### ### ### ### ##0.00")
                    End If
                Else
                    '========================= Arabe =================================================
                    If AR.Checked = True Then
                        If TextBox1.Text = "" Then
                            MySoundPlayer2.Play()
                            MsgBox("Entrer un Chiffre SVP!", MsgBoxStyle.Information, "Remarque")
                        Else
                            RichTextBox1.RightToLeft = Windows.Forms.RightToLeft.Yes
                            GroupBox2.RightToLeft = Windows.Forms.RightToLeft.Yes
                            GroupBox2.Text = "&#1576;&#1575;&#1604;&#1581;&#1585;&#1608;&#1601;"
                            RichTextBox1.Text = NoToTxt(CDbl(TextBox1.Text), 1)
                            MySoundPlayer1.Play()
                            TextBox1.Text = Format(CDbl(TextBox1.Text), "### ### ### ### ##0.00")
                        End If
                    End If
                End If
            End If
        End If

    End Sub

    Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
        If FR.Checked = True Or AR.Checked = True Then
            TextBox1.Text = ""
        End If
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        Select Case e.KeyChar
            Case ChrW(8)

            Case ChrW(46)
                e.KeyChar = ChrW(44)
            Case ChrW(44), ChrW(48) To ChrW(58)
            Case Else
                e.KeyChar = ChrW(0)
                Beep()
        End Select
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Public Function ConvNumberLetter(ByVal TheNo As Double, ByVal Langue As Byte) As String
        Dim dblEnt As Object, byDec As Byte
        Dim bNegatif As Boolean
        Dim strDev As String, strCentimes As String
        If TheNo = 0 Then
            ConvNumberLetter = "Zéro"
        Else
            If TheNo < 0 Then
                bNegatif = True
                TheNo = Math.Abs(TheNo)
            End If
            dblEnt = Int(TheNo)
            byDec = CInt((TheNo - dblEnt) * 100)
            If byDec = 0 Then
                If dblEnt > 999999999999999.0# Then
                    ConvNumberLetter = "#TropGrand"
                    Exit Function
                End If
            Else
                If dblEnt > 9999999999999.99 Then
                    ConvNumberLetter = "#TropGrand"
                    Exit Function
                End If
            End If
            strDev = " Dinar"
            strCentimes = " Centime"
            If byDec = 1 Then strCentimes = strCentimes & "."
            If byDec > 1 Then strCentimes = strCentimes & "s."
            If dblEnt > 1 Then strDev = strDev & "s"
            If byDec <> 0 Then
                ConvNumberLetter = ConvNumEnt(CDbl(dblEnt), Langue) & strDev & " et " & _
                ConvNumDizaine(byDec, Langue) & strCentimes
            Else : ConvNumberLetter = ConvNumEnt(CDbl(dblEnt), Langue) & strDev & " Algérien."
            End If
        End If
    End Function

    Private Function ConvNumEnt(ByVal TheNo As Double, ByVal Langue As Byte)
        Dim iTmp As Object, dblReste As Double
        Dim strTmp As String

        iTmp = TheNo - (Int(TheNo / 1000) * 1000)
        ConvNumEnt = ConvNumCent(CInt(iTmp), Langue)
        dblReste = Int(TheNo / 1000)
        iTmp = dblReste - (Int(dblReste / 1000) * 1000)
        strTmp = ConvNumCent(CInt(iTmp), Langue)
        Select Case iTmp
            Case 0
            Case 1
                strTmp = "Mille "
            Case Else
                strTmp = strTmp & " Mille "
        End Select
        ConvNumEnt = strTmp & ConvNumEnt
        dblReste = Int(dblReste / 1000)
        iTmp = dblReste - (Int(dblReste / 1000) * 1000)
        strTmp = ConvNumCent(CInt(iTmp), Langue)
        Select Case iTmp
            Case 0
            Case 1
                strTmp = strTmp & " Million "
            Case Else
                strTmp = strTmp & " Millions "
        End Select
        ConvNumEnt = strTmp & ConvNumEnt
        dblReste = Int(dblReste / 1000)
        iTmp = dblReste - (Int(dblReste / 1000) * 1000)
        strTmp = ConvNumCent(CInt(iTmp), Langue)
        Select Case iTmp
            Case 0
            Case 1
                strTmp = strTmp & " Milliard "
            Case Else
                strTmp = strTmp & " Milliards "
        End Select
        ConvNumEnt = strTmp & ConvNumEnt
        dblReste = Int(dblReste / 1000)
        iTmp = dblReste - (Int(dblReste / 1000) * 1000)
        strTmp = ConvNumCent(CInt(iTmp), Langue)
        Select Case iTmp
            Case 0
            Case 1
                strTmp = strTmp & " Billion "
            Case Else
                strTmp = strTmp & " Billions "
        End Select
        ConvNumEnt = strTmp & ConvNumEnt

    End Function

    Private Function ConvNumDizaine(ByVal TheNo As Byte, ByVal Langue As Byte) As String
        Dim TabUnit As Object, TabDiz As Object
        Dim byUnit As Byte, byDiz As Byte
        Dim strLiaison As String
        Dim array01() As String = {"", "Un", "Deux", "Trois", "Quatre", "Cinq", "Six", "Sept", _
            "Huit", "Neuf", "Dix", "Onze", "Douze", "Treize", "Quatorze", "Quinze", _
            "Seize", "Dix-sept", "Dix-huit", "Dix-neuf"}
        Dim array10() As String = {"", "", "Vingt", "Trente", "Quarante", "Cinquante", _
            "Soixante", "Soixante", "Quatre-vingt", "Quatre-vingt"}
        TabUnit = array01
        TabDiz = array10

        byDiz = Int(TheNo / 10)
        byUnit = TheNo - (byDiz * 10)
        strLiaison = "-"
        If byUnit = 1 Then strLiaison = " et "
        Select Case byDiz
            Case 0
                strLiaison = ""
            Case 1
                byUnit = byUnit + 10
                strLiaison = ""
            Case 7
                If Langue = 0 Then byUnit = byUnit + 10
            Case 8
                If Langue <> 2 Then strLiaison = "-"
            Case 9
                If Langue = 0 Then
                    byUnit = byUnit + 10
                    strLiaison = "-"
                End If
        End Select
        ConvNumDizaine = TabDiz(byDiz)
        If byDiz = 8 And Langue <> 2 And byUnit = 0 Then ConvNumDizaine = ConvNumDizaine & "s"
        If TabUnit(byUnit) <> "" Then
            ConvNumDizaine = ConvNumDizaine & strLiaison & TabUnit(byUnit)
        Else
            ConvNumDizaine = ConvNumDizaine
        End If
    End Function

    Private Function ConvNumCent(ByVal TheNo As Integer, ByVal Langue As Byte) As String
        Dim TabUnit As Object
        Dim byCent As Byte, byReste As Byte
        Dim strReste As String
        Dim arrray() As String = {"", "Un", "Deux", "Trois", "Quatre", "Cinq", "Six", "Sept", "Huit", "Neuf", "Dix"}
        TabUnit = arrray

        byCent = Int(TheNo / 100)
        byReste = TheNo - (byCent * 100)
        strReste = ConvNumDizaine(byReste, Langue)
        Select Case byCent
            Case 0
                ConvNumCent = strReste
            Case 1
                If byReste = 0 Then
                    ConvNumCent = "Cent"
                Else
                    ConvNumCent = "Cent " & strReste
                End If
            Case Else
                If byReste = 0 Then
                    ConvNumCent = TabUnit(byCent) & " Cent"
                Else
                    ConvNumCent = TabUnit(byCent) & " Cent " & strReste
                End If
        End Select
    End Function
    '==================================  Arabe   ==================================

    Public Function NoToTxt(ByVal TheNo As Double, ByVal Langue As Byte) As String
        Const MyCur = "&#1583;&#1610;&#1606;&#1575;&#1585;", MyCont = "&#1580;&#1586;&#1575;&#1574;&#1585;&#1610;", MySubCur = "&#1587;&#1606;&#1578;&#1610;&#1605;&#1575;"
        Dim MyArry1(0 To 9) As String
        Dim MyArry2(0 To 9) As String
        Dim MyArry3(0 To 9) As String
        Dim Myno As String
        Dim GetNo As String
        Dim RdNo As String
        Dim My100 As String
        Dim My10 As String
        Dim My1 As String
        Dim My11 As String
        Dim My12 As String
        Dim GetTxt As String
        Dim Mybillion As String
        Dim MyMiliard As String
        Dim MyMillion As String
        Dim MyThou As String
        Dim MyHun As String
        Dim MyFraction As String
        Dim MyAnd As String
        Dim I As Integer
        Dim ReMark As String

        If TheNo > 999999999999999.0# Then
            NoToTxt = "#&#1585;&#1602;&#1605; &#1603;&#1576;&#1610;&#1585; &#1580;&#1583;&#1575;"
            Exit Function
        End If

        If TheNo < 0 Then
            TheNo = TheNo * -1
            ReMark = "&#1610;&#1578;&#1576;&#1602;&#1609; &#1604;&#1603;&#1605; "
        Else
            ReMark = "."
        End If

        If TheNo = 0 Then
            NoToTxt = "&#1589;&#1601;&#1585;"
            Exit Function
        End If

        MyAnd = " &#1608;"
        MyArry1(0) = ""
        MyArry1(1) = "&#1605;&#1575;&#1574;&#1577;"
        MyArry1(2) = "&#1605;&#1575;&#1574;&#1578;&#1575;&#1606;"
        MyArry1(3) = "&#1579;&#1604;&#1575;&#1579;&#1577; &#1605;&#1575;&#1574;&#1577;"
        MyArry1(4) = "&#1571;&#1585;&#1576;&#1593;&#1577; &#1605;&#1575;&#1574;&#1577;"
        MyArry1(5) = "&#1582;&#1605;&#1587;&#1577; &#1605;&#1575;&#1574;&#1577;"
        MyArry1(6) = "&#1587;&#1578;&#1577; &#1605;&#1575;&#1574;&#1577;"
        MyArry1(7) = "&#1587;&#1576;&#1593;&#1577; &#1605;&#1575;&#1574;&#1577;"
        MyArry1(8) = "&#1579;&#1605;&#1575;&#1606;&#1610;&#1577; &#1605;&#1575;&#1574;&#1577;"
        MyArry1(9) = "&#1578;&#1587;&#1593;&#1577; &#1605;&#1575;&#1574;&#1577;"

        MyArry2(0) = ""
        MyArry2(1) = " &#1593;&#1588;&#1585;"
        MyArry2(2) = "&#1593;&#1588;&#1585;&#1608;&#1606;"
        MyArry2(3) = "&#1579;&#1604;&#1575;&#1579;&#1608;&#1606;"
        MyArry2(4) = "&#1571;&#1585;&#1576;&#1593;&#1608;&#1606;"
        MyArry2(5) = "&#1582;&#1605;&#1587;&#1608;&#1606;"
        MyArry2(6) = "&#1587;&#1578;&#1608;&#1606;"
        MyArry2(7) = "&#1587;&#1576;&#1593;&#1608;&#1606;"
        MyArry2(8) = "&#1579;&#1605;&#1575;&#1606;&#1608;&#1606;"
        MyArry2(9) = "&#1578;&#1587;&#1593;&#1608;&#1606;"

        MyArry3(0) = ""
        MyArry3(1) = "&#1608;&#1575;&#1581;&#1583;"
        MyArry3(2) = "&#1573;&#1579;&#1606;&#1575;&#1606;"
        MyArry3(3) = "&#1579;&#1604;&#1575;&#1579;&#1577;"
        MyArry3(4) = "&#1571;&#1585;&#1576;&#1593;&#1577;"
        MyArry3(5) = "&#1582;&#1605;&#1587;&#1577;"
        MyArry3(6) = "&#1587;&#1578;&#1577;"
        MyArry3(7) = "&#1587;&#1576;&#1593;&#1577;"
        MyArry3(8) = "&#1579;&#1605;&#1575;&#1606;&#1610;&#1577;"
        MyArry3(9) = "&#1578;&#1587;&#1593;&#1577;"
        '======================

        GetNo = Format(TheNo, "000000000000000.00")

        I = 0
        Do While I < 18

            If I < 15 Then
                Myno = Mid$(GetNo, I + 1, 3)
            Else
                Myno = "0" + Mid$(GetNo, I + 2, 2)
            End If

            If (Mid$(Myno, 1, 3)) > 0 Then

                RdNo = Mid$(Myno, 1, 1)
                My100 = MyArry1(RdNo)
                RdNo = Mid$(Myno, 3, 1)
                My1 = MyArry3(RdNo)
                RdNo = Mid$(Myno, 2, 1)
                My10 = MyArry2(RdNo)

                If Mid$(Myno, 2, 2) = 11 Then My11 = "&#1573;&#1581;&#1583;&#1609; &#1593;&#1588;&#1585;"
                If Mid$(Myno, 2, 2) = 12 Then My12 = "&#1573;&#1579;&#1606;&#1609; &#1593;&#1588;&#1585;"
                If Mid$(Myno, 2, 2) = 10 Then My10 = "&#1593;&#1588;&#1585;&#1577;"

                If ((Mid$(Myno, 1, 1)) > 0) And ((Mid$(Myno, 2, 2)) > 0) Then My100 = My100 + MyAnd
                If ((Mid$(Myno, 3, 1)) > 0) And ((Mid$(Myno, 2, 1)) > 1) Then My1 = My1 + MyAnd

                GetTxt = My100 + My1 + My10

                If ((Mid$(Myno, 3, 1)) = 1) And ((Mid$(Myno, 2, 1)) = 1) Then
                    GetTxt = My100 + My11
                    If ((Mid$(Myno, 1, 1)) = 0) Then GetTxt = My11
                End If

                If ((Mid$(Myno, 3, 1)) = 2) And ((Mid$(Myno, 2, 1)) = 1) Then
                    GetTxt = My100 + My12
                    If ((Mid$(Myno, 1, 1)) = 0) Then GetTxt = My12
                End If

                If (I = 0) And (GetTxt <> "") Then
                    If ((Mid$(Myno, 1, 3)) > 10) Then
                        Mybillion = GetTxt + " &#1576;&#1604;&#1610;&#1608;&#1606;"
                    Else
                        Mybillion = GetTxt + " &#1576;&#1604;&#1610;&#1608;&#1606;&#1575;&#1578;"
                        If ((Mid$(Myno, 1, 3)) = 1) Then Mybillion = " &#1576;&#1604;&#1610;&#1608;&#1606;"
                        If ((Mid$(Myno, 1, 3)) = 2) Then Mybillion = " &#1576;&#1604;&#1610;&#1608;&#1606;&#1575;&#1606;"
                    End If
                End If

                If (I = 3) And (GetTxt <> "") Then
                    If ((Mid$(Myno, 1, 3)) > 10) Then
                        MyMiliard = GetTxt + " &#1605;&#1604;&#1610;&#1575;&#1585;"
                    Else
                        MyMiliard = GetTxt + " &#1605;&#1604;&#1610;&#1575;&#1585;&#1575;&#1578;"
                        If ((Mid$(Myno, 1, 3)) = 1) Then MyMiliard = " &#1605;&#1604;&#1610;&#1575;&#1585;"
                        If ((Mid$(Myno, 1, 3)) = 2) Then MyMiliard = " &#1605;&#1604;&#1610;&#1575;&#1585;&#1575;&#1606;"
                    End If
                End If

                If (I = 6) And (GetTxt <> "") Then

                    If ((Mid$(Myno, 1, 3)) > 10) Then
                        MyMillion = GetTxt + " &#1605;&#1604;&#1610;&#1608;&#1606;"
                    Else
                        MyMillion = GetTxt + " &#1605;&#1604;&#1575;&#1610;&#1610;&#1606;"
                        If ((Mid$(Myno, 1, 3)) = 1) Then MyMillion = " &#1605;&#1604;&#1610;&#1608;&#1606;"
                        If ((Mid$(Myno, 1, 3)) = 2) Then MyMillion = " &#1605;&#1604;&#1610;&#1608;&#1606;&#1575;&#1606;"
                    End If
                End If

                If (I = 9) And (GetTxt <> "") Then
                    If ((Mid$(Myno, 1, 3)) > 10) Then
                        MyThou = GetTxt + " &#1571;&#1604;&#1601;"
                    Else
                        MyThou = GetTxt + " &#1570;&#1604;&#1575;&#1601;"
                        If ((Mid$(Myno, 2, 2)) = 1) Then MyThou = " &#1571;&#1604;&#1601;"
                        If ((Mid$(Myno, 3, 1)) = 2) Then MyThou = " &#1571;&#1604;&#1601;&#1575;&#1606;"

                    End If
                End If

                If (I = 12) And (GetTxt <> "") Then MyHun = GetTxt
                If (I = 15) And (GetTxt <> "") Then MyFraction = GetTxt
            End If

            I = I + 3
        Loop

        If (Mybillion <> "") Then
            If (MyMiliard <> "") Or (MyMillion <> "") Or (MyThou <> "") Or (MyHun <> "") Then Mybillion = Mybillion + MyAnd
        End If

        If (MyMiliard <> "") Then
            If (MyMillion <> "") Or (MyThou <> "") Or (MyHun <> "") Then MyMiliard = MyMiliard + MyAnd
        End If

        If (MyMillion <> "") Then
            If (MyThou <> "") Or (MyHun <> "") Then MyMillion = MyMillion + MyAnd
        End If

        If (MyThou <> "") Then
            If (MyHun <> "") Then MyThou = MyThou + MyAnd
        End If

        If MyFraction <> "" Then
            If (Mybillion <> "") Or (MyMiliard <> "") Or (MyMillion <> "") Or (MyThou <> "") Or (MyHun <> "") Then
                NoToTxt = Mybillion + MyMiliard + MyMillion + MyThou + MyHun + " " + MyCur + MyAnd + MyFraction + " " + MySubCur + ReMark
            Else
                NoToTxt = MyFraction + " " + MySubCur + ReMark
            End If
        Else
            NoToTxt = Mybillion + MyMiliard + MyMillion + MyThou + MyHun + " " + MyCur + " " + MyCont + ReMark
        End If

    End Function

End Class

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.