• Jueves 2 de Mayo de 2024, 02:37

Autor Tema:  Numeros A Letras  (Leído 2913 veces)

janus.ast

  • Nuevo Miembro
  • *
  • Mensajes: 20
    • Ver Perfil
Numeros A Letras
« en: Sábado 22 de Julio de 2006, 00:16 »
0
este codigo encontre cambia numeros enteros a letras pero necesito implementarlo para que cambia numero flotante  a letras con dos decimales
ayuda

janus.ast

  • Nuevo Miembro
  • *
  • Mensajes: 20
    • Ver Perfil
Re: Numeros A Letras
« Respuesta #1 en: Sábado 22 de Julio de 2006, 00:19 »
0
------------------------------------------
AnsiString Basic(int num);
AnsiString Num2Txt(double numero);

//funciona exportar
extern "C" __declspec(dllexport)AnsiString NumToTxt(double numero);

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
        return 1;
}
//---------------------------------------------------------------------------
AnsiString NumToTxt(double numero){
return Num2Txt(numero);
}
//-----------------
AnsiString Basic(int num){
AnsiString texto,numero;
AnsiString unidades[10],decenas[9],centenas[3];
AnsiString miles,millones[2],odecena[10];
int i,j,l;
char N[80];

unidades[0]="cero";
unidades[1]="uno";
unidades[2]="dos";
unidades[3]="tres";
unidades[4]="cuatro";
unidades[5]="cinco";
unidades[6]="seis";
unidades[7]="siete";
unidades[8]="ocho";
unidades[9]="nueve";

decenas[0]="diez";
decenas[1]="veinte";
decenas[2]="treinta";
decenas[3]="cuarenta";
decenas[4]="cincuenta";
decenas[5]="sesenta";
decenas[6]="setenta";
decenas[7]="ochenta";
decenas[8]="noventa";

centenas[0]="cien";
centenas[1]="ciento";
centenas[2]="cientos";
miles="mil";

millones[0]="millon";
millones[1]="millones";

odecena[0]="once";
odecena[1]="doce";
odecena[2]="trece";
odecena[3]="catorce";
odecena[4]="quince";
odecena[5]="dieciseis";
odecena[6]="diecisiete";
odecena[7]="dieciocho";
odecena[8]="diecinueve";
odecena[9]="veinti";

numero=IntToStr(num);
//longitud del numero
l=strlen(numero.c_str());
strcpy(N,strrev(numero.c_str()));

for(i=l-1;i>=0;i--){

        //traduce centenas
        if(i==2){
                j=N-48;
                if(j!=1&&j!=5&&j!=7&&j!=9)
                        texto=unidades[j]+" ";
                if(j!=1&&j!=5&&j!=7&&j!=9)
                        texto+=centenas[2]+" ";
                else{
                        switch(j){
                                case 5: texto+="quinientos "; break;
                                case 7: texto+="setecientos "; break;
                                case 9: texto+="novecientos "; break;
                                case 1: if((N[j-1]-48)!=0)
                                                texto+=centenas[1]+" ";
                                        else
                                                texto+=centenas[0];
                        }
                }
        }

        //traduce decenas
        if(i==1){
                j=N-48;
                if(j>2){
                        if((N[i-1]-48)!=0)
                                texto+=decenas[j-1]+ " y ";
                        else
                                texto+=decenas[j-1];
                }else{
                         if(j==0) continue;
                         if((N[i-1]-48)==0){
                                texto+=decenas[j-1];
                         }else{
                                if(j==2){
                                        texto+=odecena[9];
                                }else{
                                        AnsiString Temp;
                                        int t;
                                        Temp=j;
                                        Temp+=N[i-1]-48;
                                        t=StrToInt(Temp);
                                        switch(t){
                                                case 11: texto+="once"; break;
                                                case 12: texto+="doce"; break;
                                                case 13: texto+="trece"; break;
                                                case 14: texto+="catorce"; break;
                                                case 15: texto+="quince"; break;
                                                case 16: texto+="diesiseis"; break;
                                                case 17: texto+="diesisiete"; break;
                                                case 18: texto+="diesiocho"; break;
                                                case 19: texto+="diesinueve"; break;
                                        }
                                        break;
                                }
                         }
                }
        }

        //traduce unidades
        if(i==0){
                j=N-48;
                if(j!=0)
                        texto+=unidades[j];
        }
}
return texto;
}
//---------------------------------------------------------------------------

AnsiString Num2Txt(double numero){
AnsiString temp,texto;
int num[6];
int l,i;

temp=FloatToStr(numero);
l=strlen(temp.c_str());

// traduce numero menores a 1000
if(l<=3)
        texto=Basic(numero);

//traduce numero mayores a 1000 y menores a un millon
if(l>3&&l<=6){
       i=l-2;
       num[0]=StrToInt(temp.SubString(i,3));
       num[1]=StrToInt(temp.SubString(0,l-3));
       if(num[1]>1)
               texto=Basic(num[1])+ " mil ";
       else
                texto="mil ";
       texto+=Basic(num[0]);
}

//traduce numeros mayores a un millon y menores a mil millones
if(l>6&&l<=9){
       i=l-2;
       num[0]=StrToInt(temp.SubString(i,3));
       i=l-5;
       num[1]=StrToInt(temp.SubString(i,3));
       num[2]=StrToInt(temp.SubString(0,l-6));
       if(num[2]>1)
               texto=Basic(num[2])+" millones ";
       else
                texto="un millon ";
       if(num[1]!=0)
               texto+=Basic(num[1]) +" mil ";
       texto+=Basic(num[0]);
}

//traduce numeros mayores a mil millones y menores a un billon
if(l>9&&l<=12){
       i=l-2;
       num[0]=StrToInt(temp.SubString(i,3));
       i=l-5;
       num[1]=StrToInt(temp.SubString(i,3));
       i=l-8;
       num[2]=StrToInt(temp.SubString(i,3));
       num[3]=StrToInt(temp.SubString(0,l-9));
       if(num[3]!=0)
               texto=Basic(num[3]) +" mil ";
       if(num[2]>1)
               texto+=Basic(num[2])+" millones ";
       else
                texto="un millon ";
       if(num[1]!=0)
               texto+=Basic(num[1]) +" mil ";
       texto+=Basic(num[0]);
}

//regresa la cadena
return texto;
} :lightsabre:  :kicking:  :smartass:  :comp:

janus.ast

  • Nuevo Miembro
  • *
  • Mensajes: 20
    • Ver Perfil
Re: Numeros A Letras
« Respuesta #2 en: Sábado 22 de Julio de 2006, 00:48 »
0
este solo cambia enteros ayuda por favor

ArKaNtOs

  • Miembro de PLATA
  • *****
  • Mensajes: 1253
  • Nacionalidad: mx
    • Ver Perfil
Re: Numeros A Letras
« Respuesta #3 en: Sábado 22 de Julio de 2006, 07:04 »
0
yo hice uno para vb pero bien corrienton, espero te sirva :P

Public Function Nombra(ByVal num As Single, ByVal numfijo As Double) As String
num = Format(num, "00.00")
numletra = num \ 1000
If numletra > 0 And numletra < 21 Then
    letras = letras & compara(numletra) & " MIL"
    num = num - (numletra * 1000)
End If
If numletra > 20 Then
    temp = numletra
    If numletra > 20 And numletra < 30 Then
        numletra2 = Val(Right(numletra, 1))
        letras = letras & "VEINTI" & compara(numletra2) & " MIL"
    Else
        numletra2 = Val(Right(numletra, 1))
        numletra = numletra - Val(Right(numletra, 1))
        letras = letras & compara(numletra) & " Y " & compara(numletra2) & " MIL"
    End If
    numletra = temp
    num = num - (numletra * 1000)
End If
numletra = num \ 100
If numletra > 0 Then
    If numletra = 1 Then
        If num = 100 Then
            numletra = 100
            letras = letras & " " & compara(numletra)
            numletra = 1
        Else
            letras = letras & " CIENTO"
        End If
    Else
        If numletra = 5 Then
            letras = letras & " QUINIENTOS"
        ElseIf numletra = 9 Then
            letras = letras & " NOVECIENTOS"
        ElseIf numletra = 7 Then
            letras = letras & " SETECIENTOS"
        Else
            letras = letras & " " & compara(numletra) & "CIENTOS"
        End If
    End If
    num = num - (numletra * 100)
End If
num = Val(Left(num, 2))
numletra = num \ 1
If numletra > 0 Then
    temp = numletra
    If numletra > 0 And numletra <= 20 Then
        letras = letras & " " & compara(numletra)
    End If
    If numletra > 20 And numletra < 30 Then
        numletra2 = Val(Right(numletra, 1))
        letras = letras & " VEINTI" & compara(numletra2)
    ElseIf numletra > 29 Then
        numletra2 = Val(Right(numletra, 1))
        If numletra2 = 0 Then
            numletra = numletra - Val(Right(numletra, 1))
            letras = letras & " " & compara(numletra)
        ElseIf numletra2 = 1 Then
            numletra = numletra - Val(Right(numletra, 1))
            letras = letras & " " & compara(numletra) & " Y UN"
        Else
            numletra = numletra - Val(Right(numletra, 1))
            letras = letras & " " & compara(numletra) & " Y " & compara(numletra2)
        End If
    End If
    num = num - (numletra * 100)
End If
numfijo = Format(lbltotven, "00.00")
Nombra = letras & " PESOS " & Right(numfijo, 2) & " / 100 M.N."
End Function

Public Function compara(ByVal num As Single) As String
Select Case num
        Case 1
            compara = "UN"
        Case 2
            compara = "DOS"
        Case 3
            compara = "TRES"
        Case 4
            compara = "CUATRO"
        Case 5
            compara = "CINCO"
        Case 6
            compara = "SEIS"
        Case 7
            compara = "SIETE"
        Case 8
            compara = "OCHO"
        Case 9
            compara = "NUEVE"
        Case 10
            compara = "DIEZ"
        Case 11
            compara = "ONCE"
        Case 12
            compara = "DOCE"
        Case 13
            compara = "TRECE"
        Case 14
            compara = "CATORCE"
        Case 15
            compara = "QUINCE"
        Case 16
            compara = "DIESCISEIS"
        Case 17
            compara = "DIESCISIETE"
        Case 18
            compara = "DIECIOCHO"
        Case 19
            compara = "DIECINUEVE"
        Case 20
            compara = "VEINTE"
        Case 21
            compara = "VEINTIUNO"
        Case 22
            compara = "VEINTIDOS"
        Case 30
            compara = "TREINTA"
        Case 40
            compara = "CUARENTA"
        Case 50
            compara = "CINCUENTA"
        Case 60
            compara = "SESENTA"
        Case 70
            compara = "SETENTA"
        Case 80
            compara = "OCHENTA"
        Case 90
            compara = "NOVENTA"
        Case 100
            compara = "CIEN"
    End Select
End Function

wako13

  • Miembro activo
  • **
  • Mensajes: 36
    • Ver Perfil
Re: Numeros A Letras
« Respuesta #4 en: Sábado 22 de Julio de 2006, 15:59 »
0
Aqui les dejo uno que funciona a la perfección.

//---------------------------------------------------------------------------
// Funcion que convierte un numero a su representacion en letras
// por Cesar Salazar Gonzalez.
// Nota, en Mexico/LatinoAmerica, utilizamos la coma para separar miles.
// ejemplo mil quinientas pesetas con 20 centavos, se representa asi
// 1,500.20  <-Formato mexico
// 1.500,20  <-Formato español
// manera de usarla
// AnsiString Cadena = NoToLet("12335.56);

AnsiString __fastcall NoToLet(AnsiString  CANTIDAD)
{
    AnsiString NUEVA_CANTIDAD,NUMERO,DECIMALES,LETRAS="";

// PARA LIMPIAR LAS COMAS..,en formato Mex..
    NUEVA_CANTIDAD="";
    for(int ad=1;ad<=CANTIDAD.Length();ad++)
        if(CANTIDAD[ad]!=',')
            NUEVA_CANTIDAD += CANTIDAD[ad];

//extraer la parte Entera, cuando es ".", en Formato Mex
// cambiar a "," cuando sea español
    if( NUEVA_CANTIDAD.Pos(".")> 0 ){
        NUMERO = NUEVA_CANTIDAD.SubString(1,NUEVA_CANTIDAD.Pos(".")-1);
        DECIMALES =" "+NUEVA_CANTIDAD.SubString(  NUEVA_CANTIDAD.Pos(".")+1,2)+"/100 M.N";
    }else{
        NUMERO= NUEVA_CANTIDAD;
        DECIMALES = ". 00/100 M.N.";
    }
   int p=1,l=1,des;
    int LARGO,LARGO2;
//   char N1='0',N2='0',N3='0';
   char N1,N2,N3;
   LARGO=NUMERO.Length();
   LARGO2=NUMERO.Length();
   while(l<=LARGO-1){
      if(LARGO2-3==0 || LARGO2-6==0 || LARGO2-9==0){
         p=3;
         des=3;
      }
      if(LARGO2-3==-1 || LARGO2-6==-1 || LARGO2-9==-1){
         p=2;
         des=2;
      }
      if(LARGO2-3==-2 || LARGO2-6==-2 || LARGO2-9==-2){
         p=1;
         des=1;
      }
///////////////////// numeros de Cienes.
      if(p==3){
         N1=NUMERO[l];
         N2=NUMERO[l+1];
         N3=NUMERO[l+2];
         switch(N1){
            case '0':
                  l++;
                  p--;
                  break;
            case '1':
                  if(N2=='0' && N3=='0'){
                        LETRAS.Insert(" CIEN ",LETRAS.Length()+1);
                     l+=2;
                     p-=2;
                  }else{
                            LETRAS.Insert(" CIENTO ",LETRAS.Length()+1);
                     l++;
                     p--;
                  }
                  break;
            case '2':
                        LETRAS.Insert(" DOSCIENTOS ",LETRAS.Length()+1);
                  l++;
                  p--;
                  break;
            case '3':
                        LETRAS.Insert(" TRESCIENTOS ",LETRAS.Length()+1);
                  l++;
                  p--;
                  break;
            case '4':
                        LETRAS.Insert(" CUATROCIENTOS ",LETRAS.Length()+1);
                  l++;
                  p--;
                  break;
            case '5':
                        LETRAS.Insert(" QUINIENTOS ",LETRAS.Length()+1);
                  l++;
                  p--;
                  break;
            case '6':
                        LETRAS.Insert(" SEISCIENTOS ",LETRAS.Length()+1);
                  l++;
                  p--;
                  break;
            case '7':
                        LETRAS.Insert(" SETECIENTOS ",LETRAS.Length()+1);
                  l++;
                  p--;
                  break;
            case '8':
                        LETRAS.Insert(" OCHOCIENTOS ",LETRAS.Length()+1);
                  l++;
                  p--;
                  break;
            case '9':
                        LETRAS.Insert(" NOVECIENTOS ",LETRAS.Length()+1);
                  l++;
                  p--;
                  break;
         }
      }
///////////////////// numeros de decenas.
      if(p==2){
         N1=NUMERO[l];
         N2=NUMERO[l+1];
         switch(N1){
            case '0':
                  l++;
                  p--;
                  break;
            case '1':
                  if(N2!='0'){
                     switch(N2){
                        case '1':
                                        LETRAS.Insert(" ONCE ",LETRAS.Length()+1);
                              break;
                        case '2':
                                        LETRAS.Insert(" DOCE ",LETRAS.Length()+1);
                              break;
                        case '3':
                                        LETRAS.Insert(" TRECE ",LETRAS.Length()+1);
                              break;
                        case '4':
                                        LETRAS.Insert(" CATORCE ",LETRAS.Length()+1);
                              break;
                        case '5':
                                        LETRAS.Insert(" QUINCE ",LETRAS.Length()+1);
                              break;
                        case '6':
                                        LETRAS.Insert(" DIECISEIS ",LETRAS.Length()+1);
                              break;
                        case '7':
                                        LETRAS.Insert(" DIECISIETE ",LETRAS.Length()+1);
                              break;
                        case '8':
                                        LETRAS.Insert(" DIECIOCHO ",LETRAS.Length()+1);
                              break;
                        case '9':
                                        LETRAS.Insert(" DIECINUEVE ",LETRAS.Length()+1);
                              break;
                     }
                  }else{
                            LETRAS.Insert(" DIEZ ",LETRAS.Length()+1);
                        }
                  l+=2;
                  p-=2;
                  break;
            case '2':
                  if(N2!='0'){
                            LETRAS.Insert(" VEINTI",LETRAS.Length()+1);
                     l++;
                     p--;
                  }else{
                     LETRAS.Insert(" VEINTE ",LETRAS.Length()+1);
                     l+=2;
                     p-=2;
                  }
                  break;
            case '3':if(N2!='0'){
                     LETRAS.Insert(" TREINTA Y ",LETRAS.Length()+1);
                     l++;
                     p--;
                  }else{
                     LETRAS.Insert(" TREINTA ",LETRAS.Length()+1);
                            l+=2;
                     p-=2;
                  }
                  break;
            case '4':if(N2!='0'){
                     LETRAS.Insert(" CUARENTA Y ",LETRAS.Length()+1);
                     l++;
                     p--;
                  }else{
                     LETRAS.Insert(" CUARENTA ",LETRAS.Length()+1);
                     l+=2;
                     p-=2;
                  }
                  break;
            case '5':if(N2!='0'){
                     LETRAS.Insert(" CINCUENTA Y ",LETRAS.Length()+1);
                     l++;
                     p--;
                  }else{
                     LETRAS.Insert(" CINCUENTA ",LETRAS.Length()+1);
                     l+=2;
                     p-=2;
                  }
                  break;
            case '6':if(N2!='0'){
                     LETRAS.Insert(" SESENTA Y ",LETRAS.Length()+1);
                     l++;
                     p--;
                  }else{
                     LETRAS.Insert(" SESENTA ",LETRAS.Length()+1);
                     l+=2;
                     p-=2;
                  }
                  break;
            case '7':if(N2!='0'){
                     LETRAS.Insert(" SETENTA Y ",LETRAS.Length()+1);
                     l++;
                     p--;
                  }else{
                     LETRAS.Insert(" SETENTA ",LETRAS.Length()+1);
                     l+=2;
                     p-=2;
                  }
                  break;
            case '8':if(N2!='0'){
                     LETRAS.Insert(" OCHENTA Y ",LETRAS.Length()+1);
                     l++;
                     p--;
                  }else{
                     LETRAS.Insert(" OCHENTA ",LETRAS.Length()+1);
                     l+=2;
                     p-=2;
                  }
                  break;
            case '9':if(N2!='0'){
                     LETRAS.Insert(" NOVENTA Y ",LETRAS.Length()+1);
                     l++;
                     p--;
                  }else{
                     LETRAS.Insert(" NOVENTA ",LETRAS.Length()+1);
                     l+=2;
                     p-=2;
                  }
                  break;
         }
      }
///////////////////// numeros ordinarios.
      if(p==1){
         N1=NUMERO[l];
         switch(N1){
            case '0':
                  break;
            case '1':
                  LETRAS.Insert("UNO ",LETRAS.Length()+1);
                  break;
            case '2':
                        LETRAS.Insert("DOS ",LETRAS.Length()+1);
                  break;
            case '3':
                        LETRAS.Insert("TRES ",LETRAS.Length()+1);
                  break;
            case '4':
                        LETRAS.Insert("CUATRO ",LETRAS.Length()+1);
                  break;
            case '5':
                        LETRAS.Insert("CINCO ",LETRAS.Length()+1);
                  break;
            case '6':
                        LETRAS.Insert("SEIS ",LETRAS.Length()+1);
                  break;
            case '7':
                        LETRAS.Insert("SIETE ",LETRAS.Length()+1);
                  break;
            case '8':
                        LETRAS.Insert("OCHO ",LETRAS.Length()+1);
                  break;
            case '9':
                        LETRAS.Insert("NUEVE ",LETRAS.Length()+1);
                  break;
         }
         l++;
      }
      // miles
      if(N1!='0'){
             // en esta parte se pude insertar mas formatos
             // aun que creo que con billones es suficiente.

         if(LARGO2==11 || LARGO2==12 || LARGO2==10)
            if (LARGO2==7 && N1=='1')
                    LETRAS =" UN BILLON ";
             else
                    LETRAS.Insert(" BILLONES ",LETRAS.Length()+1);
         if(LARGO2==8 || LARGO2==9 || LARGO2==7)
            if (LARGO2==7 && N1=='1')
                    LETRAS = " UN MILLON "  ;
             else
                    LETRAS.Insert(" MILLONES ",LETRAS.Length()+1);
         if(LARGO2==5 || LARGO2==6 || LARGO2==4)
            if (LARGO2==4 && N1=='1')
                    LETRAS = " MIL ";
             else
                    LETRAS.Insert(" MIL ",LETRAS.Length()+1);
      }
      LARGO2-=des;
   }
    return "("+(LETRAS+"PESOS "+DECIMALES)+")";
}