Codice per convertire cifre in parole – Visual Basic VBA PHP e Javascript

Se hai bisogno di convertire un numero:

123.456.783

Nel suo corrispondente letterale:

Centoventitremilioniquattrocentocinquantaseimilasettecentottantatrè!

Il nostro codice ti potrà essere di aiuto poichè rispetta la grammatica Italiana e soddisfa fino a diciotto cifre!

Il nostro convertitore di numeri in parole (che abbiamo chiamato spell_my_int) è disponibile in diversi linguaggi: Bash, PHP, Visual Basic/VBA, e Javascript! ;)

Termini e condizioni!

I codici sorgente presenti in questa pagina sono rilasciati sotto i termini della licenza libera GNU General Public License versione 3 o successive: Hai il diritto di utilizzare, migliorare/peggiorare questo codice per qualsiasi necessità a patto che il codice da te prodotto sia chiaramente coperto dalla stessa licenza (affinchè io per esempio possa avvantaggiarmi delle tue migliorie :D ). Inoltre, ti prego di segnalare su questa pagina un link dove riporti un riferimento al tuo utilizzo, per facilitarmi appunto il mio lavoro di andare a vedere come la gente usa e migliora il mio codice :)

Il codice funziona. In ogni caso (come per ogni software Libero) non diamo alcuna garanzia o responsabilità :)

Grazie, e buon uso!

Versione Bash per GNU/Linux

Il nostro script per linea di comando Bash sui sistemi GNU/Linux (e credo Mac OS X) è disponibile su Launchpad con il nome di n2t-it (number to text – Italiano).
Per prima cosa occorre installare bzr da utente root:

apt-get install bzr

Quindi da utente normale bisogna scaricare il sorgente ed entrare nella cartella:

bzr branch lp:n2t-it
cd n2t-it

A quel punto si può usarlo (ha molte opzioni):

# Per la pagina di manuale
./n2t -h
 
# Esempio minimale
./n2t 123.45
 
# Esempio completo da STDIN
echo 123/44 | ./n2t -c "/" -t " virgola " -d

Versione per Microsoft Windows©

Screenshot spell_my_int() - Visual Basic - Reyboz Blog

Puoi scaricare il programma spell_my_int per Windows da questo indirizzo.
Oppure puoi vedere il codice del programma (in Visual Basic) scorrendo più in basso.

Versione in PHP

Puoi vedere la versione in PHP online su questo indirizzo.
Oppure puoi usarlo sul tuo sito personale inserendo nella tua pagina questo codice:

<iframe src="http://lab.reyboz.it/spell_my_int/PHP/?number=0" width="250" height="150" />
  <p>Your browser does not support iframes.</p>
</iframe>

Puoi inoltre scorgere il codice scorrendo più in basso.

Versione in Javascript

Visualizzalo online in JavascriptVedi codice Javascript

spell_my_int() in Visual Basic

'	Convertitore di numeri interi in corrispettivi letterali.
'	Esempio:	123 => centoventitrè
'	Born:	10 Giugno 2012
'	Licenza: GNU General Public License (versione 3 o successive)
'	Autore: Valerio Bozzolan - Reyboz.it
Public Function spell_my_int(ByVal num As Long, Optional ByVal centOOttanta As Boolean = False) As String
	Dim numstring As String = num.ToString
	Dim numlen As Integer
	Dim IDcent As Byte
	Dim subnumerostring As String
	Dim subnumero As Integer
	Dim cifra(2) As String
	Dim text(2) As String
	Dim prime2cifre As Integer
	Dim result As String = ""
	Dim sezione As Integer = 0
	Dim mono() As String = {"", "uno", "due", "tre", "quattro", "cinque", "sei", "sette", "otto", "nove"}
	Dim duplo() As String = {"dieci", "undici", "dodici", mono(3) & "dici", "quattordici", "quindici", "sedici", "dicias" & mono(7), "dici " & mono(8), "dician" & mono(9)}
	Dim deca() As String = {"", duplo(0), "venti", mono(3) & "nta", "quaranta", "cinquanta", "sessanta", "settanta", "ottanta", "novanta"}
	Dim cento() As String = {"cent", "cento"}
	Dim mili(,) As String = {{"", "mille", "milione", "miliardo", "bilione", "biliardo"}, {"", "mila", "milioni", "miliardi", "bilioni", "biliardi"}}
	Dim max As Long = (10 ^ (6 * 3) - 1) - 1
	If (num < 0) Then
		Return "Minimo 0!"
	ElseIf (num > max) Then
		Return "Massimo " & max & "!"
	ElseIf (num = 0) Then
		Return "zero"
	End If
	Select Case (numstring.Length Mod 3)
		Case 1
			numstring = "00" & num.ToString
		Case 2
			numstring = "0" & num.ToString
		Case Else
			numstring = num.ToString
	End Select
	numlen = numstring.Length
	'MsgBox("Numlen: " & numlen & "  subnumero: " & numstring)
	Do While ((sezione + 1) * 3 <= numlen)
		subnumerostring = Mid(numstring, (numlen - ((sezione + 1) * 3)) + 1, 3)
		subnumero = CInt(subnumerostring)
		cifra(0) = Mid(subnumerostring, 1, 1)
		cifra(1) = Mid(subnumerostring, 2, 1)
		cifra(2) = Mid(subnumerostring, 3, 1)
		'MsgBox("Sezione numero: " & sezione & ", cifre della sezione: " & cifra(0) & cifra(1) & cifra(2))
		If (subnumero <> 0) Then
			prime2cifre = CInt(cifra(1)) * 10 + CInt(cifra(2))
			'MsgBox(prime2cifre)
			If (prime2cifre < 10) Then
				text(2) = mono(cifra(2))
				text(1) = ""
			ElseIf (prime2cifre < 20) Then
				text(2) = ""
				text(1) = duplo(prime2cifre - 10)
			Else
				'ventitre => ventitrè
				If (sezione = 0 And cifra(2) = 3) Then
					text(2) = "trè"
				Else
					text(2) = mono(cifra(2))
				End If
				'	novantaotto => novantotto
				If (cifra(2) = 1 Or cifra(2) = 8) Then
					text(1) = Mid(deca(cifra(1)), 1, deca(cifra(1)).Length - 1)
				Else
					text(1) = deca(cifra(1))
				End If
			End If
			If (cifra(0) = 0) Then
				text(0) = ""
			Else
				' centoottanta => centottanta
				If (Not centOOttanta And cifra(1) = 8 Or (cifra(1) = 0 And cifra(2) = 8)) Then
					IDcent = 0
				Else
					IDcent = 1
				End If
				If (cifra(0) <> 1) Then
					text(0) = mono(cifra(0)) & cento(IDcent)
				Else
					text(0) = cento(IDcent)
				End If
			End If
			'	unomille	=> mille
			'	miliardo	=> unmiliardo
			If (subnumero = 1 And sezione <> 0) Then
				If (sezione >= 2) Then
					result = "un" & mili(0, sezione) & result
				Else
					result = mili(0, sezione) & result
				End If
			Else
				result = text(0) & text(1) & text(2) & mili(1, sezione) & result
			End If
		End If
		sezione = sezione + 1
	Loop
	Return result
End Function

spell_my_int() in VBA (Visual Basic for Application)

'	Convertitore di numeri interi in corrispettivi letterali.
'	Esempio:	123 => centoventitrè
'	Born:	10 Giugno 2012
'	Licenza: GNU General Public License (versione 3 o successive)
'	Autore: Valerio Bozzolan - Reyboz.it
Function spell_my_int(ByVal num As Long) As String
    Dim centOOttanta As Boolean
    centOOttanta = false
    Dim numstring As String
    Dim numlen As Integer
    Dim IDcent As Byte
    Dim subnumerostring As String
    Dim subnumero As Integer
    Dim cifra(2) As String
    Dim text(2) As String
    Dim prime2cifre As Integer
    Dim result As String
    Dim sezione As Integer
    Dim mono(9) As String
    Dim duplo(9) As String
    Dim deca(9) As String
    Dim cento(2) As String
    Dim mili(1, 9) As String
    Dim max
    max = ((10 ^ (6 * 3)) - 1) - 1
    '   Caricamento di mono() in formato esteso
    mono(0) = ""
    mono(1) = "uno"
    mono(2) = "due"
    mono(3) = "tre"
    mono(4) = "quattro"
    mono(5) = "cinque"
    mono(6) = "sei"
    mono(7) = "sette"
    mono(8) = "otto"
    mono(9) = "nove"
    '   Caricamento di duplo() in formato esteso
    duplo(0) = "dieci"
    duplo(1) = "undici"
    duplo(2) = "dodici"
    duplo(3) = "tredici"
    duplo(4) = "quattordici"
    duplo(5) = "quindici"
    duplo(6) = "sedici"
    duplo(7) = "dicias" & mono(7)
    duplo(8) = "dici" & mono(8)
    duplo(9) = "dician" & mono(9)
    '   Caricamento di deca() in formato esteso
    deca(0) = ""
    deca(1) = duplo(0)
    deca(2) = "venti"
    deca(3) = mono(3) & "nta"
    deca(4) = "quaranta"
    deca(5) = "cinquanta"
    deca(6) = "sessanta"
    deca(7) = "settanta"
    deca(8) = "ottanta"
    deca(9) = "novanta"
    '   Caricamento di cento() in formato esteso
    cento(0) = "cent"
    cento(1) = cento(0) & "o"
    '   Caricamento di mili(0 ,) in formato esteso
    mili(0, 0) = ""
    mili(0, 1) = "mille"
    mili(0, 2) = "milione"
    mili(0, 3) = "miliardo"
    mili(0, 4) = "bilione"
    mili(0, 5) = "biliardo"
    '   Caricamento di mili(1 ,) in formato esteso
    mili(1, 0) = ""
    mili(1, 1) = "mila"
    mili(1, 2) = "milioni"
    mili(1, 3) = "miliardi"
    mili(1, 4) = "bilioni"
    mili(1, 5) = "biliardi"
    result = ""
    sezione = 0
    If (num < 0) Then
        spell_my_int = "Minimo 0!"
    ElseIf (num > max) Then
        spell_my_int = "Massimo " & max & "!"
    ElseIf (num = 0) Then
        spell_my_int = "zero"
    Else
        numstring = CStr(num)
        Select Case (Len(numstring) Mod 3)
            Case 1
                numstring = "00" & CStr(num)
            Case 2
                numstring = "0" & CStr(num)
            Case Else
                numstring = CStr(num)
        End Select
        numlen = Len(numstring)
        'MsgBox ("Numlen: " & numlen & "  subnumero: " & numstring)
        Do While ((sezione + 1) * 3 <= numlen)
            subnumerostring = Mid(numstring, (numlen - ((sezione + 1) * 3)) + 1, 3)
            subnumero = CInt(subnumerostring)
            cifra(0) = Mid(subnumerostring, 1, 1)
            cifra(1) = Mid(subnumerostring, 2, 1)
            cifra(2) = Mid(subnumerostring, 3, 1)
            'MsgBox ("Sezione numero: " & sezione & ", cifre della sezione: " & cifra(0) & cifra(1) & cifra(2))
            If (subnumero <> 0) Then
                prime2cifre = CInt(cifra(1)) * 10 + CInt(cifra(2))
                'MsgBox (prime2cifre)
                If (prime2cifre < 10) Then
                    text(2) = mono(cifra(2))
                    text(1) = ""
                ElseIf (prime2cifre < 20) Then
                    text(2) = ""
                    text(1) = duplo(prime2cifre - 10)
                Else
                    'ventitre => ventitrè
                    If (sezione = 0 And cifra(2) = 3) Then
                        text(2) = "trè"
                    Else
                        text(2) = mono(cifra(2))
                    End If
                    '   novantaotto => novantotto
                    If (cifra(2) = 1 Or cifra(2) = 8) Then
                        text(1) = Mid(deca(cifra(1)), 1, Len(deca(cifra(1))) - 1)
                    Else
                        text(1) = deca(cifra(1))
                    End If
                End If
                If (cifra(0) = 0) Then
                    text(0) = ""
                Else
                    ' centoottanta => centottanta
                    If (Not centOOttanta And cifra(1) = 8 Or (cifra(1) = 0 And cifra(2) = 8)) Then
                        IDcent = 0
                    Else
                        IDcent = 1
                    End If
                    If (cifra(0) <> 1) Then
                        text(0) = mono(cifra(0)) & cento(IDcent)
                    Else
                        text(0) = cento(IDcent)
                    End If
                End If
                '   unomille    => mille
                '   miliardo    => unmiliardo
                If (subnumero = 1 And sezione <> 0) Then
                    If (sezione >= 2) Then
                        result = "un" & mili(0, sezione) & result
                    Else
                        result = mili(0, sezione) & result
                    End If
                Else
                    result = text(0) & text(1) & text(2) & mili(1, sezione) & result
                End If
            End If
            sezione = sezione + 1
        Loop
        spell_my_int = result
    End If
End Function

spell_my_int() in PHP

/*
	Convertitore di numeri interi in corrispettivi letterali.
	Esempio:	123 => centoventitrè
	Born:	10 Giugno 2012
	Licenza: GNU General Public License (versione 3 o successive)
	Autore: Valerio Bozzolan - Reyboz.it
*/
function spell_my_int($num, $centOOttanta=false) {
	$num	= (int) $num;
	$mono	= array("", "uno", "due", "tre", "quattro", "cinque", "sei", "sette", "otto", "nove");
	$duplo	= array("dieci", "undici", "dodici", "{$mono[3]}dici", "quattordici", "quindici", "sedici", "dicias{$mono[7]}", "dici{$mono[8]}", "dician{$mono[9]}");
	$deca	= array("", $duplo[0], "venti", "{$mono[3]}nta", "quaranta", "cinquanta", "sessanta", "settanta", "ottanta", "novanta");
	$cento	= array("cent", "cento");
	$mili	= array(
		0 => array("", "mille", "milione", "miliardo", "bilione", "biliardo"),
		1 => array("", "mila", "milioni", "miliardi", "bilioni", "biliardi")
	);
	$max	= pow(10, count($mili[0]) * 3) - 1;
	if( !is_numeric($num) ) {
		return "Non &egrave; un numero!";
	} elseif ( $num < 0 ) {
		return "Numero negativo!";
	} elseif ( $num > $max ) {
		return "Limite superato!";
	} elseif( $num == 0 ) {
		return "zero";
	}
	$result	= "";
	$sezione	= 0;
	$num	= (string) $num;
	switch( strlen($num) % 3 ) {
		case 1:	$num	= "00$num";
			break;
		case 2:	$num	= "0$num";
	}
	$numlen	= strlen($num);
	while( ($sezione + 1) * 3 <= $numlen ) {
		$cifra	= substr($num, (($numlen - 1) - (($sezione + 1) * 3)) + 1, 3);
		$numero	= (int) $cifra;
		$cifra[0]	= (int) $cifra[0];
		$cifra[1]	= (int) $cifra[1];
		$cifra[2]	= (int) $cifra[2];
		if( $numero <> 0 ) { 
			$prime2cifre	= (int) ($cifra[1] . $cifra[2]);
			if( $prime2cifre < 10 ) {
				$text[2]	= $mono[$cifra[2]];
				$text[1]	= "";
			} elseif( $prime2cifre < 20 ) {
				$text[2]	= "";
				$text[1]	= $duplo[$prime2cifre - 10];
			} else {
				//	ventitre => ventitrè
				if( $sezione == 0 && $cifra[2] == 3 ) {
					$text[2]	= "tr&egrave;";
				} else {
					$text[2]	= $mono[$cifra[2]];
				}
				//	novantaotto => novantotto
				if( $cifra[2] == 1 || $cifra[2] == 8 ) {
					$text[1]	= substr($deca[$cifra[1]], 0, -1);
				} else {
					$text[1]	= $deca[$cifra[1]];
				}
			}
			if( $cifra[0] == 0 ) {
				$text[0]	= "";
			} else {
				//	centoottanta => centottanta
				if( !$centOOttanta && $cifra[1] == 8 || ($cifra[1] == 0 && $cifra[2] == 8) ) {
					$IDcent	= 0;
				} else {
					$IDcent	= 1;
				}
				if( $cifra[0] <> 1 ) {
					$text[0]	= $mono[$cifra[0]] . $cento[$IDcent];
				} else {
					$text[0]	= $cento[$IDcent];
				}
			}
			//	unomille	=> mille
			//	miliardo	=> unmiliardo
			if( $numero == 1 && $sezione <> 0 ) {
				if( $sezione >= 2 ) {
					$result	= "un" . $mili[0][$sezione] . $result;
				} else {
					$result	= $mili[0][$sezione] . $result;
				}
			} else {
				$result	= $text[0] . $text[1] . $text[2] . $mili[1][$sezione] . $result;
			}
		}
		$sezione++;
	}
	return $result;
}

spell_my_int() in Javascript

/*
	Convertitore di numeri interi in corrispettivi letterali.
	Esempio:	123 => centoventitre'
	Born:	10 Giugno 2012
	Licenza: GNU General Public License (versione 3 o successive)
	Autore: Valerio Bozzolan - Reyboz.it
*/
function spell_my_int(numstr, centOOttanta) {
	mono	= new Array ("", "uno", "due", "tre", "quattro", "cinque", "sei", "sette", "otto", "nove");
	duplo	= new Array ("dieci", "undici", "dodici", mono[3] + "dici", "quattordici", "quindici", "sedici", "dicias" + mono[7], "dici" + mono[8], "dician" + mono[9]);
	deca	= new Array ("", duplo[0], "venti", mono[3] + "nta", "quaranta", "cinquanta", "sessanta", "settanta", "ottanta", "novanta");
	cento	= new Array ("cent", "cento");
	mili	= new Array ();
	mili[0]	= new Array ("", "mille", "milione", "miliardo", "bilione", "biliardo");
	mili[1]	= new Array ("", "mila", "milioni", "miliardi", "bilioni", "biliardi");
	text	= new Array ();
	cifra	= new Array ();
	result	= "";
	sezione	= 0;
	// In Javascript si fa così per dire che questo parametro e' opzionale, con valore false di default
	centOOttanta = centOOttanta || false;
	numstr += '';
	// Non deve cominciare per zero altrimenti parseInt() impazzisce...
	while ( numstr.substr(0, 1) == "0" && numstr.length != 1 ) {
		numstr	= numstr.substr(1, numstr.length);
	}
	num	= parseInt(numstr);
	switch( numstr.length % 3 ) {
		case 1:	numstr	= "00" + numstr;
			break;
		case 2:	numstr	= "0" + numstr;
	}
	numlen	= numstr.length;
	if( isNaN(num) ) {
		return "Non e' un numero!";
	} else if ( num < 0 ) {
		return "Numero negativo!";
	} else if ( numstr.length > 6 * 3 ) {
		return "Limite superato!";
	} else if( num == 0 ) {
		return "zero";
	}
	while( (sezione + 1) * 3 <= numlen ) {
		subnumerostring = numstr.substr(((numlen - 1) - ((sezione + 1) * 3)) + 1, 3);
		if( subnumerostring != "000" ) {
			subnumero = parseInt(subnumerostring);
			cifra[0] = subnumerostring.substr(0, 1);
			cifra[1] = subnumerostring.substr(1, 1);
			cifra[2] = subnumerostring.substr(2, 1);
			prime2cifre	= parseInt(cifra[1] * 10) + parseInt(cifra[2]);
			if( prime2cifre < 10 ) {
				text[2]	= mono[cifra[2]];
				text[1]	= "";
			} else if( prime2cifre < 20 ) {
				text[2]	= "";
				text[1]	= duplo[prime2cifre - 10];
			} else {
				//	ventitre => ventitrè
				if( sezione == 0 && cifra[2] == 3 ) {
					text[2]	= "tre'";
				} else {
					text[2]	= mono[cifra[2]];
				}
				//	novantaotto => novantotto
				if( cifra[2] == 1 || cifra[2] == 8 ) {
					text[1]	= deca[cifra[1]].substr(0, deca[cifra[1]].length -1);
				} else {
					text[1]	= deca[cifra[1]];
				}
			}
			if( cifra[0] == 0 ) {
				text[0]	= "";
			} else {
				//	centoottanta => centottanta
				if( !centOOttanta && cifra[1] == 8 || (cifra[1] == 0 && cifra[2] == 8) ) {
					IDcent	= 0;
				} else {
					IDcent	= 1;
				}
				if( cifra[0] != 1 ) {
					text[0]	= mono[cifra[0]] + cento[IDcent];
				} else {
					text[0]	= cento[IDcent];
				}
			}
			//	unomille	=> mille
			//	miliardo	=> unmiliardo
			if( subnumero == 1 && sezione != 0 ) {
				if( sezione >= 2 ) {
					result	= "un" + mili[0][sezione] + result;
				} else {
					result	= mili[0][sezione] + result;
				}
			} else {
				result	= text[0] + text[1] + text[2] + mili[1][sezione] + result;
			}
		}
		sezione++;
	}
	return result;
}

Utilizzi pratici di spell_my_int()

Utilizzo di spell_my_int() in Visual Basic:

 
' Scrivere qui il codice lungo di spell_my_int() in Visual Basic!
 
Sub prova()
	dim num as long
	dim num_in_parole as string
	num = 123
	num_in_parole = spell_my_int(num)
	msgbox(num_in_parole)
End Sub

Utilizzo di spell_my_int() in una macro VBA:

 
' Scrivere qui il codice lungo di spell_my_int() in VBA!!!
 
Sub prova()
	dim num as long
	dim num_in_parole as string
	num = 123
	num_in_parole = spell_my_int(num)
	msgbox(num_in_parole)
End Sub

Utilizzo di spell_my_int() dentro una cella di Excel: (prima bisogna copiare il codice lungo di spell_my_int() nel modulo “nomemodulo”)

=nomemodulo.spell_my_int(123)

Utilizzo di spell_my_int() in PHP:

<?php
 
// Scrivere qui il codice lungo di spell_my_int() in PHP!
 
$num	= 123;
$num_in_parole = spell_my_int($num);
echo $num_in_parole;
?>

Utilizzo di spell_my_int() in Javascript:

<script type="text/javascript">
 
// Scrivere qui il codice lungo di spell_my_int() in Javascript!
 
num = 123;
num_in_parole = spell_my_int(num);
alert(num_in_parole);
</script>