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
|
Option Explicit
Public Function Nombres_En_Lettres_US(Nombre As String, Optional monaie As String = ",")
Dim Unit1, Unit10, Ms, dec, i#, x#, a#, N1$, N2$, N3$, NinT, Part$, NbL$, tabl, N#, T#, unité$
Unit1 = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Height", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Heighteen", "Nineteen")
Unit10 = Array("", "Ten", "Twenty", "Thirty", "Forty", "Fifty ", "Sixty", "Seventy", "Eighty", "Ninety", "Hundred")
Ms = Array("", "quadrillion", "trillion", "Billion", "Million", "Thousand", ""):
dec = Split(Replace(Nombre, ".", ","), ",")
If UBound(dec) > 0 Then dec(1) = Right("00" & Round(Val("0." & dec(1)), 2) * 100, 3)
If monaie = "," And UBound(dec) = 0 Then monaie = ""
monaie = IIf(monaie <> "," And monaie <> "" And Val(dec(0)) > 1, monaie & "s", monaie)
For i = 0 To UBound(dec)
If Len(dec(i)) Mod 3 <> 0 Then dec(i) = Application.Rept("0", 3 - Len(dec(i)) Mod 3) & dec(i)
For N = 1 To Len(dec(i)) Step 3
Part = Mid(dec(i), N, 3):
N1 = Val(Mid(Part, 1, 1)): N2 = Mid(Part, 2, 1): N3 = Mid(Part, 3, 1): NinT = Mid(Part, 2, 2)
If NinT > 10 And NinT < 20 Then N2 = 0: N3 = N3 + 10
NbL = NbL & Unit1(N1) & IIf(N1 <> 0, " hundred ", "")
NbL = NbL & Unit10(N2) & IIf(N3 <> 0 And N2 <> 0, "-", IIf(Left(Part, 2) = "10" And N3 = 1, " and ", " "))
NbL = NbL & Unit1(N3) & " "
NbL = NbL & Chr(1)
Next
If i = 0 Then
tabl = Split(NbL, Chr(1))
a = UBound(Ms) - UBound(tabl) + 1
For T = 0 To UBound(tabl) - 1
If Trim(tabl(T)) <> "" Then unité = Ms(a + x) Else unité = ""
tabl(T) = tabl(T) & unité & IIf(unité <> "" And Trim(tabl(T)) <> "One" And tabl(T) <> "", "s", "")
x = x + 1
Next
NbL = Join(tabl, " ") & monaie
NbL = NbL & IIf(UBound(dec) > 0 And monaie <> ",", " & ", "")
End If
Next
Nombres_En_Lettres_US = Trim(Replace(Replace(NbL, " ", ""), " ", " "))
End Function |
Partager