' Copyright (C) 2025  P4pillon.org
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU Affero General Public License as
' published by the Free Software Foundation, either version 3 of the
' License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Affero General Public License for more details.
'
' You should have received a copy of the GNU Affero General Public License
' along with this program.  If not, see <https://www.gnu.org/licenses/>.
Public Class Nombres_En_Lettres
    Public Function converti(ByVal chiffre As Double) As String
        Dim centaine As Integer
        Dim dizaine As Integer
        Dim unite As Integer
        Dim reste As Integer
        Dim y As Integer
        Dim dix As Boolean = False
        Dim lettre As String = ""


        reste = chiffre / 1

        Dim i As Integer = 1000000000
        Do

            y = reste / i

            '******************************
            If (y <> 0) Then

                centaine = Int(y / 100)
                dizaine = Int((y - centaine * 100) / 10)
                unite = y - (centaine * 100) - (dizaine * 10)
                '*******************
                Select Case centaine
                    Case 0

                    Case 1
                        lettre = lettre + "cent "

                    Case 2
                        If ((dizaine = 0) And (unite = 0)) Then
                            lettre = lettre + "deux cents "
                        Else
                            lettre = lettre + "deux cent "
                        End If

                    Case 3
                        If ((dizaine = 0) And (unite = 0)) Then
                            lettre = lettre + "trois cents "
                        Else
                            lettre = lettre + "trois cent "
                        End If

                    Case 4
                        If ((dizaine = 0) And (unite = 0)) Then
                            lettre = lettre + "quatre cents "
                        Else
                            lettre = lettre + "quatre cent "
                        End If

                    Case 5
                        If ((dizaine = 0) And (unite = 0)) Then
                            lettre = lettre + "cinq cents "
                        Else
                            lettre = lettre + "cinq cent "
                        End If

                    Case 6
                        If ((dizaine = 0) And (unite = 0)) Then
                            lettre = lettre + "six cents "
                        Else
                            lettre = lettre + "six cent "
                        End If

                    Case 7
                        If ((dizaine = 0) And (unite = 0)) Then
                            lettre = lettre + "sept cents "
                        Else
                            lettre = lettre + "sept cent "
                        End If

                    Case 8
                        If ((dizaine = 0) And (unite = 0)) Then
                            lettre = lettre + "huit cents "
                        Else
                            lettre = lettre + "huit cent "
                        End If

                    Case 9
                        If ((dizaine = 0) And (unite = 0)) Then
                            lettre = lettre + "neuf cents "
                        Else
                            lettre = lettre + "neuf cent "
                        End If
                End Select
                '*****************************
                '********************************
                Select Case dizaine
                    Case 0

                    Case 1
                        dix = True

                    Case 2
                        lettre = lettre + "vingt "

                    Case 3
                        lettre = lettre + "trente "

                    Case 4
                        lettre = lettre + "quarante "

                    Case 5
                        lettre = lettre + "cinquante "

                    Case 6
                        lettre = lettre + "soixante "

                    Case 7
                        dix = True
                        lettre = lettre + "soixante "

                    Case 8
                        lettre = lettre + "quatre-vingt "

                    Case 9
                        dix = True
                        lettre = lettre + "quatre-vingt "
                End Select
                '*******************************
                '********************************
                Select Case unite
                    Case 0
                        If (dix) Then
                            lettre = lettre + "dix "
                        End If

                    Case 1
                        If (dix) Then
                            lettre = lettre + "onze "
                        Else
                            lettre = lettre + "un "
                        End If

                    Case 2
                        If (dix) Then
                            lettre = lettre + "douze "
                        Else
                            lettre = lettre + "deux "
                        End If

                    Case 3
                        If (dix) Then
                            lettre = lettre + "treize "
                        Else
                            lettre = lettre + "trois "
                        End If

                    Case 4
                        If (dix) Then
                            lettre = lettre + "quatorze "
                        Else
                            lettre = lettre + "quatre "
                        End If

                    Case 5
                        If (dix) Then
                            lettre = lettre + "quinze "
                        Else
                            lettre = lettre + "cinq "
                        End If

                    Case 6
                        If (dix) Then
                            lettre = lettre + "seize "
                        Else
                            lettre = lettre + "six "
                        End If

                    Case 7
                        If (dix) Then
                            lettre = lettre + "dix-sept "
                        Else
                            lettre = lettre + "sept "
                        End If

                    Case 8
                        If (dix) Then
                            lettre = lettre + "dix-huit "
                        Else
                            lettre = lettre + "huit "
                        End If

                    Case 9
                        If (dix) Then
                            lettre = lettre + "dix-neuf "
                        Else
                            lettre = lettre + "neuf "
                        End If
                End Select
                '*********************************
                '**********************************
                Select Case i
                    Case 1000000000
                        If (y > 1) Then
                            lettre = lettre + "milliards "
                        Else
                            lettre = lettre + "milliard "
                        End If

                    Case 1000000
                        If (y > 1) Then
                            lettre = lettre + "millions "
                        Else
                            lettre = lettre + "million "
                        End If

                    Case 1000
                        lettre = lettre + "mille "
                End Select
                '**************************************
            End If
            '************************************
            reste = reste - y * i
            dix = False

            i /= 1000
        Loop Until i < 1


        If (Len(lettre) = 0) Then
            lettre = lettre + "zero"
        End If

        Return lettre

    End Function

End Class