N�meros en Palabras

Fecha: 27/Oct/2004 (26/Oct/2004)
Autor: Hugo Contreras [email protected]

 


Quer�a compartir con ustedes esta funci�n que devuelve un numero en palabras. El numero a ingresar tiene un tama�o m�ximo de 999.999.999.999.999. O sea hasta billones. Creo que deber� ser suficiente en la mayor�a de los casos.

Muy �til para la emisi�n de cheques, facturas, etc.

Ej: Importe = 123456789

En Palabras = CIENTO VEINTITRES MILLONES CUATROCIENTOS CINCUENTA Y SEIS MIL SETECIENTOS OCHENTA Y NUEVE DE PESOS CON 00/100

Espero que les sirva.
Saludos

PD: No olviden calificar este articulo. Me servir� para obtener la cuarta estrella (M�ndenme un email)

A continuaci�n sigue c�digo en Visual Basic:

Para que puedan hacer pruebas copien todo el c�digo, creen un proyecto nuevo de Windows en VB, reemplacen el c�digo generado por el que he incluido aqu� y.... a probar! Suerte!
Option Explicit On 
Imports System.Math
Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        'This call is required by the Windows Form Designer.
        InitializeComponent()
        'Add any initialization after the InitializeComponent() call
    End Sub
    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Panel2 As System.Windows.Forms.Panel
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Panel2 = New System.Windows.Forms.Panel
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(160, 32)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = ""
        '
        'Panel1
        '
        Me.Panel1.Controls.Add(Me.TextBox2)
        Me.Panel1.Location = New System.Drawing.Point(24, 176)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(248, 128)
        Me.Panel1.TabIndex = 1
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(8, 16)
        Me.TextBox2.Multiline = True
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.TextBox2.Size = New System.Drawing.Size(232, 96)
        Me.TextBox2.TabIndex = 0
        Me.TextBox2.Text = ""
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(48, 32)
        Me.Label1.Name = "Label1"
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "Importe"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(96, 152)
        Me.Label2.Name = "Label2"
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Monto en Palabras"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(110, 112)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(72, 24)
        Me.Button1.TabIndex = 4
        Me.Button1.Text = "Test"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(110, 336)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(72, 24)
        Me.Button2.TabIndex = 5
        Me.Button2.Text = "Salir"
        '
        'Panel2
        '
        Me.Panel2.Location = New System.Drawing.Point(16, 16)
        Me.Panel2.Name = "Panel2"
        Me.Panel2.Size = New System.Drawing.Size(264, 80)
        Me.Panel2.TabIndex = 7
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 382)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Panel2)
        Me.Name = "Form1"
        Me.Text = "Numero en Letras"
        Me.Panel1.ResumeLayout(False)
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    ' Aca comienza la funcion
    Dim cMontext As String
    Public Function NumPalabra(ByVal Numero As Double) As String
        Dim Numerofmt As String
        Dim centenas As Integer
        Dim pos As Integer
        Dim cen As Integer
        Dim dec As Integer
        Dim uni As Integer
        Dim textuni As String
        Dim textdec As String
        Dim textcen As String
        Dim milestxt As String
        Dim monedatxt As String
        Dim txtPalabra As String

        Numerofmt = Format(Numero, "000000000000000.00") 'Le da un formato fijo
        centenas = 1
        pos = 1
        txtPalabra = ""
        Do While centenas <= 5
            ' extrae series de Centena, Decena, Unidad
            cen = Val(Mid(Numerofmt, pos, 1))
            dec = Val(Mid(Numerofmt, pos + 1, 1))
            uni = Val(Mid(Numerofmt, pos + 2, 1))
            pos = pos + 3
            textcen = Centena(uni, dec, cen)
            textdec = Decena(uni, dec)
            textuni = Unidad(uni, dec)
            ' determina separador de miles/millones
            Select Case centenas
                Case 1
                    If cen + dec + uni = 1 Then
                        milestxt = "Billon "
                    ElseIf cen + dec + uni > 1 Then
                        milestxt = "Billones "
                    End If
                Case 2
                    If cen + dec + uni >= 1 And Val(Mid(Numerofmt, 7, 3)) = 0 Then
                        milestxt = "Mil Millones "
                    ElseIf cen + dec + uni >= 1 Then
                        milestxt = "Mil "
                    End If
                Case 3
                    If cen + dec = 0 And uni = 1 Then
                        milestxt = "Millon "
                    ElseIf cen > 0 Or dec > 0 Or uni > 1 Then
                        milestxt = "Millones "
                    End If
                Case 4
                    If cen + dec + uni >= 1 Then
                        milestxt = "Mil "
                    End If
                Case 5
                    If cen + dec + uni >= 1 Then
                        milestxt = ""
                    End If
            End Select
            centenas = centenas + 1
            'va formando el texto del importe en palabras
            txtPalabra = txtPalabra + textcen + textdec + textuni + milestxt
            milestxt = ""
            textuni = ""
            textdec = ""
            textcen = ""
        Loop
        ' agrega denominacion de moneda
        Select Case Val(Numerofmt)
            Case 0
                monedatxt = "Cero Pesos "
            Case 1
                monedatxt = "Peso "
            Case Is < 1000000
                monedatxt = "Pesos "
            Case Is >= 1000000
                monedatxt = "de Pesos "
        End Select
        txtPalabra = txtPalabra & monedatxt & "con " & Mid(Numerofmt, 17) & "/100"
        Return txtPalabra.ToUpper
    End Function
    Private Function Centena(ByVal uni As Integer, ByVal dec As Integer, _
        ByVal cen As Integer) As String
        Select Case cen
            Case 1
                If dec + uni = 0 Then
                    cMontext = "cien "
                Else
                    cMontext = "ciento "
                End If
            Case 2 : cMontext = "doscientos "
            Case 3 : cMontext = "trescientos "
            Case 4 : cMontext = "cuatrocientos "
            Case 5 : cMontext = "quinientos "
            Case 6 : cMontext = "seiscientos "
            Case 7 : cMontext = "setecientos "
            Case 8 : cMontext = "ochocientos "
            Case 9 : cMontext = "novecientos "
            Case Else : cMontext = ""
        End Select
        Centena = cMontext
        cMontext = ""
    End Function

    Private Function Decena(ByVal uni As Integer, ByVal dec As Integer) As String
        Select Case dec
            Case 1
                Select Case uni
                    Case 0 : cMontext = "diez "
                    Case 1 : cMontext = "once "
                    Case 2 : cMontext = "doce "
                    Case 3 : cMontext = "trece "
                    Case 4 : cMontext = "catorce "
                    Case 5 : cMontext = "quince "
                    Case 6 To 9 : cMontext = "dieci"
                End Select
            Case 2
                If uni = 0 Then
                    cMontext = "veinte "
                ElseIf uni > 0 Then
                    cMontext = "veinti"
                End If
            Case 3 : cMontext = "treinta "
            Case 4 : cMontext = "cuarenta "
            Case 5 : cMontext = "cincuenta "
            Case 6 : cMontext = "sesenta "
            Case 7 : cMontext = "setenta "
            Case 8 : cMontext = "ochenta "
            Case 9 : cMontext = "noventa "
            Case Else : cMontext = ""
        End Select
        If uni > 0 And dec > 2 Then cMontext = cMontext + "y "
        Decena = cMontext
        cMontext = ""
    End Function

    Private Function Unidad(ByVal uni As Integer, ByVal dec As Integer) As String
        If dec <> 1 Then
            Select Case uni
                Case 1 : cMontext = "un "
                Case 2 : cMontext = "dos "
                Case 3 : cMontext = "tres "
                Case 4 : cMontext = "cuatro "
                Case 5 : cMontext = "cinco "
            End Select
        End If
        Select Case uni
            Case 6 : cMontext = "seis "
            Case 7 : cMontext = "siete "
            Case 8 : cMontext = "ocho "
            Case 9 : cMontext = "nueve "
        End Select
        Unidad = cMontext
        cMontext = ""
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Dispose(True)
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox2.Text = NumPalabra(Val(TextBox1.Text))
    End Sub

End Class
 

la Luna del Guille o... el Guille que está en la Luna... tanto monta...