Usuario
Decimales a texto con formato

Pregunta
-
Estimados muy buenas tardes, tengo una duda, como puedo hacer para que un numero decimal se convierta a texto y que tenga cierto formato, por ejemplo
Textbox1 = 123.12
en el Textbox2 deberia decir
"Son ciento veintitres soles con doce centimos"
Muchas gracias de ante mano.
Todas las respuestas
-
Para convertir de números a letras existen varias implementaciones, qui te dejo una que te puede ayudar
https://varionet.wordpress.com/2007/11/29/convertir-numeros-a-letras/
para formatear el texto puedes usar uno de los formatos que te da vb.net con el toString () (string.format)
https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
Saludos
Ing. Carlos Monroy MCP, MCAD, MCSD, MCTS
-
Hola
Yo uso un modulo en Excel (VBA) que talves te srva
Function CONVERTIRNUM(Numero As Double, Optional CentimosEnLetra As Boolean) As String Dim Moneda As String Dim Monedas As String Dim Centimo As String Dim Centimos As String Dim Preposicion As String Dim NumCentimos As Double Dim Letra As String Const Maximo = 1999999999.99 '************************************************************ ' Parámetros '************************************************************ Moneda = "Peso" 'Nombre de Moneda (Singular) Monedas = "Pesos" 'Nombre de Moneda (Plural) Centimo = "Centavo" 'Nombre de Céntimos (Singular) Centimos = "Centavos" 'Nombre de Céntimos (Plural) Preposicion = "Con" 'Preposición entre Moneda y Céntimos '************************************************************ 'Validar que el Numero está dentro de los límites If (Numero >= 0) And (Numero <= Maximo) Then Letra = NUMERORECURSIVO((Fix(Numero))) 'Convertir el Numero en letras 'Si Numero = 1 agregar leyenda Moneda (Singular) If (Numero = 1) Then Letra = Letra & " " & Moneda 'De lo contrario agregar leyenda Monedas (Plural) Else Letra = Letra & " " & Monedas End If NumCentimos = Round((Numero - Fix(Numero)) * 100) 'Obtener los centimos del Numero 'Si NumCentimos es mayor a cero inicar la conversión If NumCentimos >= 0 Then 'Si el parámetro CentimosEnLetra es VERDADERO obtener letras para los céntimos If CentimosEnLetra Then Letra = Letra & " " & Preposicion & " " & NUMERORECURSIVO(Fix(NumCentimos)) 'Convertir los céntimos en letra 'Si NumCentimos = 1 agregar leyenda Centimos (Singular) If (NumCentimos = 1) Then Letra = Letra & " " & Centimo 'De lo contrario agregar leyenda Centimos (Plural) Else Letra = Letra & " " & Centimos End If 'De lo contrario mostrar los céntimos como número Else If NumCentimos < 10 Then Letra = Letra & " 0" & NumCentimos & "/100" Else Letra = Letra & " " & NumCentimos & "/100" End If End If End If 'Regresar el resultado final de la conversión CONVERTIRNUM = Letra Else 'Si el Numero no está dentro de los límites, entivar un mensaje de error CONVERTIRNUM = "ERROR: El número excede los límites." End If End Function Function NUMERORECURSIVO(Numero As Long) As String Dim Unidades, Decenas, Centenas Dim Resultado As String '************************************************** ' Nombre de los números '************************************************** Unidades = Array("", "Un", "Dos", "Tres", "Cuatro", "Cinco", "Seis", "Siete", "Ocho", "Nueve", "Diez", "Once", "Doce", "Trece", "Catorce", "Quince", "Dieciséis", "Diecisiete", "Dieciocho", "Diecinueve", "Veinte", "Veintiuno", "Veintidos", "Veintitres", "Veinticuatro", "Veinticinco", "Veintiseis", "Veintisiete", "Veintiocho", "Veintinueve") Decenas = Array("", "Diez", "Veinte", "Treinta", "Cuarenta", "Cincuenta", "Sesenta", "Setenta", "Ochenta", "Noventa", "Cien") Centenas = Array("", "Ciento", "Doscientos", "Trescientos", "Cuatrocientos", "Quinientos", "Seiscientos", "Setecientos", "Ochocientos", "Novecientos") '************************************************** Select Case Numero Case 0 Resultado = "Cero" Case 1 To 29 Resultado = Unidades(Numero) Case 30 To 100 Resultado = Decenas(Numero \ 10) + IIf(Numero Mod 10 <> 0, " y " + NUMERORECURSIVO(Numero Mod 10), "") Case 101 To 999 Resultado = Centenas(Numero \ 100) + IIf(Numero Mod 100 <> 0, " " + NUMERORECURSIVO(Numero Mod 100), "") Case 1000 To 1999 Resultado = "Mil" + IIf(Numero Mod 1000 <> 0, " " + NUMERORECURSIVO(Numero Mod 1000), "") Case 2000 To 999999 Resultado = NUMERORECURSIVO(Numero \ 1000) + " Mil" + IIf(Numero Mod 1000 <> 0, " " + NUMERORECURSIVO(Numero Mod 1000), "") Case 1000000 To 1999999 Resultado = "Un Millón" + IIf(Numero Mod 1000000 <> 0, " " + NUMERORECURSIVO(Numero Mod 1000000), "") Case 2000000 To 1999999999 Resultado = NUMERORECURSIVO(Numero \ 1000000) + " Millones" + IIf(Numero Mod 1000000 <> 0, " " + NUMERORECURSIVO(Numero Mod 1000000), "") End Select NUMERORECURSIVO = Resultado End Function
Suerte
Pedro López
-
Gracias por los aportes, les dejo lo que yo encontré quizás también les sirva
con eso transformo los numeros a textos
Public Function Num2Text(ByVal value As Double) As String Select Case value Case 0 : Num2Text = "CERO" Case 1 : Num2Text = "UN" Case 2 : Num2Text = "DOS" Case 3 : Num2Text = "TRES" Case 4 : Num2Text = "CUATRO" Case 5 : Num2Text = "CINCO" Case 6 : Num2Text = "SEIS" Case 7 : Num2Text = "SIETE" Case 8 : Num2Text = "OCHO" Case 9 : Num2Text = "NUEVE" Case 10 : Num2Text = "DIEZ" Case 11 : Num2Text = "ONCE" Case 12 : Num2Text = "DOCE" Case 13 : Num2Text = "TRECE" Case 14 : Num2Text = "CATORCE" Case 15 : Num2Text = "QUINCE" Case Is < 20 : Num2Text = "DIECI" & Num2Text(value - 10) Case 20 : Num2Text = "VEINTE" Case Is < 30 : Num2Text = "VEINTI" & Num2Text(value - 20) Case 30 : Num2Text = "TREINTA" Case 40 : Num2Text = "CUARENTA" Case 50 : Num2Text = "CINCUENTA" Case 60 : Num2Text = "SESENTA" Case 70 : Num2Text = "SETENTA" Case 80 : Num2Text = "OCHENTA" Case 90 : Num2Text = "NOVENTA" Case Is < 100 : Num2Text = Num2Text(Int(value \ 10) * 10) & " Y " & Num2Text(value Mod 10) Case 100 : Num2Text = "CIEN" Case Is < 200 : Num2Text = "CIENTO " & Num2Text(value - 100) Case 200, 300, 400, 600, 800 : Num2Text = Num2Text(Int(value \ 100)) & "CIENTOS" Case 500 : Num2Text = "QUINIENTOS" Case 700 : Num2Text = "SETECIENTOS" Case 900 : Num2Text = "NOVECIENTOS" Case Is < 1000 : Num2Text = Num2Text(Int(value \ 100) * 100) & " " & Num2Text(value Mod 100) Case 1000 : Num2Text = "MIL" Case Is < 2000 : Num2Text = "MIL " & Num2Text(value Mod 1000) Case Is < 1000000 : Num2Text = Num2Text(Int(value \ 1000)) & " MIL" If value Mod 1000 Then Num2Text = Num2Text & " " & Num2Text(value Mod 1000) Case 1000000 : Num2Text = "UN MILLON" Case Is < 2000000 : Num2Text = "UN MILLON " & Num2Text(value Mod 1000000) Case Is < 1000000000000.0# : Num2Text = Num2Text(Int(value / 1000000)) & " MILLONES " If (value - Int(value / 1000000) * 1000000) Then Num2Text = Num2Text & " " & Num2Text(value - Int(value / 1000000) * 1000000) Case 1000000000000.0# : Num2Text = "UN BILLON" Case Is < 2000000000000.0# : Num2Text = "UN BILLON " & Num2Text(value - Int(value / 1000000000000.0#) * 1000000000000.0#) Case Else : Num2Text = Num2Text(Int(value / 1000000000000.0#)) & " BILLONES" If (value - Int(value / 1000000000000.0#) * 1000000000000.0#) Then Num2Text = Num2Text & " " & Num2Text(value - Int(value / 1000000000000.0#) * 1000000000000.0#) End Select End Function
y con esto le doy el formato para que me aparezca el numero decimal a texto (esto lo hago dentro de un boton que se va a encargar de transformar el valor que coloque en un textbox (120.12) a otro textbox que mostrara el texto (son ciento veinte con 12/100 nuevos soles)
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Dim a() As String a = Split(txtsubtot.Text, ".") txtson.Text = "SON " + Num2Text(a(0)) + " CON " + a(1) + "/100 NUEVOS SOLES" Dim asd As Double = CDbl(txtsubtot.Text) Dim qwe As Double = CDbl(txtdscto.Text) Dim xyz As Double = (asd * qwe) / 100 txtdescuento.Text = Format(xyz, "#,##0.00") Dim igv As Double = CDbl(txtsubtot.Text) * 0.18 txtigv.Text = Format(igv, "#,##0.00") Dim totalf As Double = CDbl(txtsubtot.Text) - CDbl(txtdescuento.Text) + CDbl(txtigv.Text) txttotal.Text = Format(totalf, "#,##0.00") End Sub
por si quieren transformar todo a texto al valor de "a(1)" por "Num2Text(a(1))"
Saludos