codice:
Private Sub Command1_Click()
Text1 = NtoStr(Text1)
End Sub
Function NtoStr(N As Single) As String
Dim Sn As String, result As String
Dim nMigliaia As String, nMilioni As String, nMiliardi As String
If N > 9999999 Then GoTo over
Sn = Str(N)
'La virgola rimane invariata
If InStr(1, Sn, ".") = 0 Then GoTo interi
result = Right(Sn, Len(Sn) - InStr(1, Sn, "."))
Sn = Left(Sn, InStr(1, Sn, ".") - 1)
result = "/" & result
interi:
result = Me.Risolvi3Cifre(Right(Sn, 3)) & result
mille:
Sn = Trim(Sn)
If Len(Sn) < 4 Then GoTo fine
If Len(Sn) < 6 Then Sn = String(6 - Len(Sn), "0") & Sn
nMigliaia = Mid(Sn, Len(Sn) - 5, 3)
If nMigliaia = "001" Then result = "mille" & result: GoTo milione
result = Me.Risolvi3Cifre(nMigliaia) & "mila" & result
If a = 7 Then a = 4
milione:
Sn = Trim(Sn)
If Len(Sn) < 7 Then GoTo fine
If Len(Sn) < 9 Then Sn = String(9 - Len(Sn), "0") & Sn
nMilioni = Mid(Sn, Len(Sn) - 8, 3)
If nMilioni = "001" Then result = "unmilione" & result: GoTo miliardo
result = Me.Risolvi3Cifre(nMilioni) & "milioni" & result
miliardo:
Sn = Trim(Sn)
If Len(Sn) < 10 Then GoTo fine
If Len(Sn) < 12 Then Sn = String(12 - Len(Sn), "0") & Sn
nMiliardi = Mid(Sn, Len(Sn) - 11, 3)
If nMiliardi = "001" Then result = "unmiliardo" & result: GoTo fine
result = Me.Risolvi3Cifre(nMiliardi) & "miliardi" & result
fine:
NtoStr = result
Exit Function
over:
Call MsgBox("Si è verificato un errore." & vbCrLf & "E' possibile che il numero sia troppo grande (Deve essere minore di 10.000.000).", vbCritical)
End Function
Public Function CifraToStrUnita(c As String) As String
Dim res As String
Select Case c
Case "1"
res = "uno"
Case "2"
res = "due"
Case "3"
res = "tre"
Case "4"
res = "quattro"
Case "5"
res = "cinque"
Case "6"
res = "sei"
Case "7"
res = "sette"
Case "8"
res = "otto"
Case "9"
res = "nove"
Case "0"
res = "zero"
End Select
CifraToStrUnita = res
End Function
Public Function CifraToStrDecine(c As String) As String
Dim res As String
Select Case c
Case "1"
res = "uno"
Case "2"
res = "venti"
Case "3"
res = "trenta"
Case "4"
res = "quaranta"
Case "5"
res = "cinquanta"
Case "6"
res = "sessanta"
Case "7"
res = "settanta"
Case "8"
res = "ottanta"
Case "9"
res = "novanta"
Case "0"
res = "zero"
End Select
CifraToStrDecine = res
End Function
Public Function CifraToStrMin20(c As String) As String
Dim res As String
Select Case c
Case "11"
res = "undici"
Case "12"
res = "dodici"
Case "13"
res = "tredici"
Case "4"
res = "quattordici"
Case "5"
res = "quindici"
Case "6"
res = "sedici"
Case "7"
res = "diciassette"
Case "8"
res = "diciotto"
Case "9"
res = "diciannove"
Case "10"
res = "dieci"
End Select
CifraToStrMin20 = res
End Function
Public Function Risolvi3Cifre(Sn As String) As String
Dim result As String
Dim sUnita As String, nUnita As String
Dim nDecine As String, nCentinaia As String
nUnita = Right(Sn, 1)
If Len(Sn) < 2 Then
nDecine = "0"
result = Me.CifraToStrUnita(nUnita) & result
GoTo fine
Else
nDecine = Mid(Sn, Len(Sn) - 1, 1)
End If
If Trim(nDecine) = "" Then nDecine = "0"
If nDecine = "1" Then
result = Me.CifraToStrMin20(nDecine & nUnita) & result
'Da 20 a 99
ElseIf nDecine <> "0" Then
'10,20,30,40,50,...
If nUnita = "0" Then
result = Me.CifraToStrDecine(nDecine) & result
'Tutti gli altri
Else
'*********CONTROLLO VOCALI*********
If Vocale(Left(Me.CifraToStrUnita(nUnita), 1)) And _
Vocale(Right(Me.CifraToStrDecine(nDecine))) Then
result = Left(Me.CifraToStrDecine(nDecine), Len(Me.CifraToStrDecine(nDecine) - 1)) & Me.CifraToStrUnita(nUnita) & result
Else
result = Me.CifraToStrDecine(nDecine) & Me.CifraToStrUnita(nUnita) & result
End If
End If
ElseIf nDecine = "0" Then
If Mid(Sn, Len(Sn) - 2, 1) = "0" And nUnita = "0" Then
result = "zero"
GoTo fine
ElseIf nUnita = "0" And Mid(Sn, Len(Sn) - 2, 1) = "1" Then
result = "cento"
GoTo fine
ElseIf Mid(Sn, Len(Sn) - 2, 1) = "0" And nUnita <> "0" Then
result = Me.CifraToStrUnita(nUnita) & result
GoTo fine
ElseIf Mid(Sn, Len(Sn) - 2, 1) <> "0" And nUnita <> "0" Then
result = Me.CifraToStrUnita(nUnita) & result
End If
End If
If Len(Sn) < 3 Then
GoTo fine
Else
nCentinaia = Mid(Sn, Len(Sn) - 2, 1)
End If
If nCentinaia = "0" Or Trim(nCentinaia) = "" Then GoTo fine
If nCentinaia = "1" Then result = "cento" & result: GoTo fine
result = Me.CifraToStrUnita(nCentinaia) & "cento" & result
fine:
Risolvi3Cifre = result
End Function
Public Function Vocale(c As String) As Boolean
Select Case c
Case "a", "e", "i", "o", "u"
Vocale = True
Case Else
Vocale = False
End Select
End Function