|
Info |
|
|
|
Esempi |
|
|
|
Linguaggio |
|
|
|
Librerie |
|
|
|
Formule |
|
|
|
|
|
|
|
|
|
Ultimi Inseriti |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Un esempio classico di problema che può essere risolto scrivendo una funzione è quello di trasformare un numero scritto in cifre nel corrispondente testo in lettere. E' anche un esempio di una di quelle funzione che fa sempre comodo avere e che può essere insierita in un raccoglitore unico.
La funzione non è semplicissima da leggere, deve tenere conto dei molti casi del plurale e singolare per i numeri.
Per prima cosa una sotto - fuznione che ci permette di avere il testo dei numeri da 1 a 19 , è anche un buon esempio di come si possano utilizzare i vettori per migliorare la leggibilit à del codice . E ' definita " privata " ovvero non visibile fuori dal modulo .
Private Function TestoCifra( Cifra As Long) As String
& nbsp ;& nbsp ; Testo = Array(" zero ", " uno ", " due ", " tre ", " quattro ", " cinque ", " sei ", " sette ", " otto ", " nove ", " dieci ", _
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;" undici ", " dodici ", " tredici ", " quattordici ", " quindici ", " sedici ", " diciassette ", " diciotto ", " diciannove ")
& nbsp ;& nbsp ; TestoCifra = Testo ( Cifra)
End Function
Private Function TestoDecina( Decina As Long, Unita As Integer) As String
& nbsp ;& nbsp ; Testo1 = Array("", "", " venti ", " trenta ", " quaranta ", " cinquanta ", " sessanta ", " settanta ", " ottanta ", " novanta ", " cento ")
& nbsp ;& nbsp ; Testo2 = Array("", "", " vent ", " trent ", " quarant ", " cinquant ", " sessant ", " settant ", " ottant ", " novant ", " cento ")
& nbsp ;& nbsp ;
& nbsp ;& nbsp ; If ( Unita = 1 Or Unita = 8 ) Then
& nbsp ;& nbsp ;& nbsp ;& nbsp ; TestoDecina = Testo2 ( Decina / 10 )
& nbsp ;& nbsp ; Else
& nbsp ;& nbsp ;& nbsp ;& nbsp ; TestoDecina = Testo1 ( Decina / 10 )
& nbsp ;& nbsp ; End If
End Function
Function ValoreInLettere( Valore As Long) As String
& nbsp ;& nbsp ; Dim res As String
& nbsp ;& nbsp ;& nbsp ;& nbsp ;
& nbsp ;& nbsp ; Select Case Valore
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Case Is < 20
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = TestoCifra ( Valore)
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Case Is < 101
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; If ( Round ( Valore / 10 ) * 10 ) = Valore Then
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = TestoDecina ( Valore , 0 )
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; Else
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = TestoDecina ( Round (( Valore - 5 ) / 10 ) * 10 , Valore - Round (( Valore - 5 ) / 10 , 0 ) * 10 ) & ValoreInLettere( Valore - Round (( Valore - 5 ) / 10 , 0 ) * 10 )
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; End If
& nbsp ;& nbsp ;& nbsp ;& nbsp ;
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Case Is < 200
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = " cento " & ValoreInLettere( Valore - 100 )
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Case Is < 1000
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; If Round ( Valore / 100 ) * 100 = Valore Then
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = ValoreInLettere( Valore / 100 ) & " cento "
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; Else
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = ValoreInLettere( Round (( Valore - 50 ) / 100 )) & " cento " & ValoreInLettere( Valore - Round (( Valore - 50 ) / 100 ) * 100 )
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; End If
& nbsp ;& nbsp ;& nbsp ;& nbsp ;
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Case 1000
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = " mille "
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Case Is < 2000
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = " mille " & ValoreInLettere( Valore - 1000 )
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Case Is < 1000000
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; If Round ( Valore / 1000 ) * 1000 = Valore Then
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = ValoreInLettere( Valore / 1000 ) & " mila "
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; Else
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = ValoreInLettere( Round (( Valore - 500 ) / 1000 )) & " mila " & ValoreInLettere( Valore - Round (( Valore - 500 ) / 1000 ) * 1000 )
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; End If
& nbsp ;& nbsp ;& nbsp ;& nbsp ;
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Case 1000000
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = " unmilione "
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Case Is < 2000000
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = " unmilione " & ValoreInLettere( Valore - Round (( Valore - 500000 ) / 1000000 ) * 1000000 )
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Case Is < 1000000000
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; If Round ( Valore / 1000000 ) * 1000000 = Valore Then
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = ValoreInLettere( Valore / 1000000 ) & " milioni "
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; Else
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = ValoreInLettere( Round (( Valore - 500000 ) / 1000000 )) & " milioni " & ValoreInLettere( Valore - Round (( Valore - 500000 ) / 1000000 ) * 1000000 )
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; End If
& nbsp ;& nbsp ;& nbsp ;& nbsp ;
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Case 1000000000
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = " unmiliardo "
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Case Is < 2000000000
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = " unmiliardo " & ValoreInLettere( Valore - Round (( Valore - 500000 ) / 1000000 ) * 1000000 )
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Case Is < 1E + 15
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; If Round ( Valore / 1000000000 ) * 1000000000 = Valore Then
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = ValoreInLettere( Valore / 1000000000 ) & " miliardi "
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; Else
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; res = ValoreInLettere( Round (( Valore - 500000000 ) / 1000000000 )) & " miliardi " & ValoreInLettere( Valore - Round (( Valore - 500000000 ) / 1000000000 ) * 1000000000 )
& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ;& nbsp ; End If
& nbsp ;& nbsp ; End Select
& nbsp ;& nbsp ; ValoreInLettere = res
End Function
Function ValoreInLettereConDec( Valore As Double) As String
& nbsp ;& nbsp ; Dim ParteIntera As Long
& nbsp ;& nbsp ; Dim ParteDecimale As Integer
& nbsp ;& nbsp ; Dim Centesimi As String
& nbsp ;& nbsp ; ParteIntera = Int( Valore)
& nbsp ;& nbsp ; ParteDecimale = Round (( Valore - Round ( Valore)) * 100 )
& nbsp ;& nbsp ; If ( ParteDecimale < 0 ) Then
& nbsp ;& nbsp ;& nbsp ;& nbsp ; ParteDecimale = ParteDecimale + 100
& nbsp ;& nbsp ; End if
& nbsp ;& nbsp ;
& nbsp ;& nbsp ; If ( ParteDecimale < 10 ) Then
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Centesimi = " 0 " & Trim( Str( ParteDecimale ))
& nbsp ;& nbsp ; Else
& nbsp ;& nbsp ;& nbsp ;& nbsp ; Centesimi = Trim( Str( ParteDecimale ))
& nbsp ;& nbsp ; End If
& nbsp ;& nbsp ; ValoreInLettereConDec = ValoreInLettere ( ParteIntera ) & "/" & Centesimi
End Function
|
|
|