Option Explicit
Function nBlettre_methode_globale(nombres As String, Optional ByVal sstr As String = "virgule", Optional ByVal finance As Boolean = False)
Dim en_dec(2), unit1, unit10, ms, cms As Long, decs As Long, ex As Long, ddd As String, centi As String, e As Long, i As Long, a As Long, dix As Long
Dim nombre As String, u As String, c As String, ct As String, et As String, ss As String, neg As Boolean
unit1 = Array("", " Un", " Deux", " Trois", " Quatre", " Cinq", " Six", " Sept", " Huit", " Neuf", " Dix", " Onze", " Douze", " treize", " Quatorze", " Quinze", " Seize", " Dix-Sept", " Dix-Huit", " Dix-Neuf", " cent", " zéro")
unit10 = Array("", " dix", " vingt", " trente", " quarante", " cinquante", " soixante", " soixante-dix", " quatre-vingt", " quatre-vingt-dix", " cent")
ms = Array("", " sextillion", " Quintillion", " Quatrillion", " Trillion", " Billiard", " Billion", " milliard", " million", " mille", ""): cms = UBound(ms)
If Left(nombres, 1) = "-" Then nombres = Mid(nombres, 2, Len(nombres)): neg = True
decs = 0: nombres = Replace(nombres, ".", ","): en_dec(0) = Split(nombres, ",")(0): If InStr(nombres, ",") > 0 Then en_dec(1) = Split(nombres, ",")(1): decs = 1 'on separe le decimal de l'entier
If Len(en_dec(0)) Mod 3 <> 0 Then en_dec(0) = Application.Rept("0", 3 - Len(en_dec(0)) Mod 3) & en_dec(0) 'on formate l'entier a 3 chiffre par tranche
If decs = 1 Then en_dec(1) = Right("00" & Round(Val("0." & en_dec(1)), 2) * 100, 3) ' NOUVELLE METHODE POUR ADAPTER LE DECIMAL on formate a 3 chiffres
ex = cms - (Len(en_dec(0)) / 3) + 1 ' index de point de depart des expressions dans l'array ms
ddd = IIf(Val(en_dec(0)) > 999000 And Val(Right(en_dec(0), 6)) = 0, IIf("aAeEiIoOuUyY" Like "*" & Left(sstr, 1) & "*", " d' ", " de"), " ")
centi = IIf(sstr <> "dollar", " centime", " cent")
If sstr = "virgule" Then centi = ""
sstr = IIf(Val(en_dec(0)) > 1, sstr & "s", sstr)
If decs = 1 Then centi = IIf(Val(en_dec(1)) > 1, centi & "s", centi)
For e = 0 To decs
For i = 1 To Len(en_dec(e)) Step 3
a = ex + Round(i / 3) 'position actuelle de ms
nombre = Mid(en_dec(e), i, 3) ' la tranche
dix = Mid(nombre, 2, 1): u = Right(nombre, 1): c = Left(nombre, 1): If c > 1 Then c = c: ct = unit1(20) & IIf(Val(dix & u) > 0, "", "s") Else: ct = "": If c = 1 Then c = 20
If dix = 1 Or dix = 7 Or dix = 9 And Right(u, 1) > 0 Then dix = dix - 1: u = u + 10 'on corrige le 1,7,9
If dix > 1 And dix <> 8 And Right(u, 1) = 1 Then et = " et" Else: If dix = 0 Or u = 0 Then et = "" Else et = "-" ' on accorde de 1 a 99
If u = 0 Then If dix = 8 Then If ms(a) = " mille" Then et = "" Else et = "s" 'le s a quatre-vingt tout seul
If nombre = 0 And Len(en_dec(0)) = 3 Then u = 21: dix = 0 ' le zéro si l'entier vaut 0 tout simplement
If nombre = 0 And i <> 1 Then a = 0
If nombre = 1 And i = 1 And a = cms - 1 Then u = 0
If e = 0 And nombre > 1 And a < cms - 1 Then ss = "s" Else ss = ""
nBlettre_methode_globale = nBlettre_methode_globale & Replace(unit1(c) & ct & unit10(dix) & et & unit1(u), "- ", "-") & IIf(e = 0, ms(a), "") & ss
Next i
If finance = False Then
nBlettre_methode_globale = nBlettre_methode_globale & IIf(e = 0 And decs = 1, " virgule ", "")
Else
nBlettre_methode_globale = nBlettre_methode_globale & IIf(e = 0 And decs = 1, ddd & " " & sstr & " et ", IIf(decs = 0, " " & sstr, "")) & IIf(e = 1, centi, "")
End If
Next e
If neg = True Then nBlettre_methode_globale = "moins " & nBlettre_methode_globale
End Function
Sub tes2()
MsgBox nBlettre_methode_globale(256354.2153, "euro", True)
MsgBox nBlettre_methode_globale(10000000.2153, "euro", True)
MsgBox nBlettre_methode_globale(10000000.2153, "Dirham", True)
MsgBox nBlettre_methode_globale(10000000.2153, "dollar", True)
MsgBox nBlettre_methode_globale(1.1, "Dirham", True)
MsgBox nBlettre_methode_globale(12563.2365)
MsgBox nBlettre_methode_globale(1.01)
End Sub