FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Petici贸n: c贸digo fuente de funci贸n Num2Letras
Posts: 139
Joined: Sun Apr 23, 2006 09:00 AM
Petici贸n: c贸digo fuente de funci贸n Num2Letras
Posted: Tue Dec 25, 2012 12:30 PM

Hola a todos/as y feliz navidad antes que nada.
Hace a帽os consegu铆 una funci贸n para Clipper que me permit铆a convertir en texto el importe que le pasaba para que apareciera en los talones (ej: 14.000 -> catorce mil). Desgraciadamente no localizo ese c贸digo y me hace falta para un programa que estoy desarrollando, as铆 que si alguien lo tiene y es tan amable de pas谩rmelo se lo agradecer茅 mucho.
Un saludo, reitero las felicidades y que sigamos escribiendo buen c贸digo el a帽o que viene y tengamos xBase para rato (que seguro lo hay).

Un saludo,

Fernando

Las Palmas de Gran Canaria
Posts: 1710
Joined: Tue Oct 28, 2008 06:26 PM
Re: Petici贸n: c贸digo fuente de funci贸n Num2Letras
Posted: Tue Dec 25, 2012 12:41 PM
Saludos,



Adhemar C.
Posts: 1054
Joined: Sun Oct 09, 2005 10:41 PM
Re: Petici贸n: c贸digo fuente de funci贸n Num2Letras
Posted: Wed Dec 26, 2012 03:08 AM
Aqui va....
Code (fw): Select all Collapse
//-------------------------------------------------------------------------------------------------------
Function Letras(Arg1, Arg2)      // el numero y el genero
local agrupos[5], aunidad[10], adecena[10, 10], acentena[10]
local aconector[5], cenletra, ngrupo, cnumstr
local cunidad, cdecena, ccentena, ccentavos, cdecim
Arg2 := If(Arg2 = Nil, Arg2:= "M", Upper(Arg2) )
If (ValType(Arg1) != "N" .OR. ValType(Arg2) != "C")
 Return ""
Endif
cdecim := (Arg1 - Int(Arg1)) * 100
ccentavos := " y " + StrZero(cdecim, 2) + "/100"
Arg1 := Int(Arg1)    // solo el entero

aunidad[1]:= {|| iif(Arg1 = 0 .AND. ngrupo = 1, "cero", "")}
aunidad[2]:= {|| iif(cdecena = "1", adecena[2][Val(cunidad) + 1], ;
   iif(agrupos[ngrupo] = "001" .AND. (ngrupo = 2 .OR. ngrupo = ;
      4), "", iif(ngrupo > 2, "un ", iif(Arg2 = "F", "una ", ;
      "uno "))))}
aunidad[3]:= {|| iif(cdecena = "1", adecena[2][Val(cunidad) + 1], ;
      "dos ")}
aunidad[4]:= {|| iif(cdecena = "1", adecena[2][Val(cunidad) + 1], ;
      "tres ")}
aunidad[5]:= {|| iif(cdecena = "1", adecena[2][Val(cunidad) + 1], ;
      "cuatro ")}
aunidad[6]:= {|| iif(cdecena = "1", adecena[2][Val(cunidad) + 1], ;
      "cinco ")}
aunidad[7]:= {|| iif(cdecena = "1", adecena[2][Val(cunidad) + 1], ;
      "seis ")}
aunidad[8]:= {|| iif(cdecena = "1", adecena[2][Val(cunidad) + 1], ;
      "siete ")}
aunidad[9]:= {|| iif(cdecena = "1", adecena[2][Val(cunidad) + 1], ;
      "ocho ")}
aunidad[10]:= {|| iif(cdecena = "1", ;
      adecena[2][Val(cunidad) + 1], "nueve ")}
adecena[1][1]:= {|| ""}
adecena[2][1]:= {|| iif(cunidad = "0", "diez ", "")}
adecena[2][2]:= "once "
adecena[2][3]:= "doce "
adecena[2][4]:= "trece "
adecena[2][5]:= "catorce "
adecena[2][6]:= "quince "
adecena[2][7]:= "dieciseis "
adecena[2][8]:= "diecisiete "
adecena[2][9]:= "dieciocho "
adecena[2][10]:= "diecinueve "
adecena[3][1]:= {|| iif(cunidad = "0", "veinte ", "veinti")}
adecena[4][1]:= {|| "treinta " + iif(cunidad != "0", "y ", "")}
adecena[5][1]:= {|| "cuarenta " + iif(cunidad != "0", "y ", "")}
adecena[6][1]:= {|| "cincuenta " + iif(cunidad != "0", "y ", "")}
adecena[7][1]:= {|| "sesenta " + iif(cunidad != "0", "y ", "")}
adecena[8][1]:= {|| "setenta " + iif(cunidad != "0", "y ", "")}
adecena[9][1]:= {|| "ochenta " + iif(cunidad != "0", "y ", "")}
adecena[10][1]:= {|| "noventa " + iif(cunidad != "0", "y ", "")}
acentena[1]:= {|| ""}
acentena[2]:= {|| iif(cdecena + cunidad = "00", "cien ", ;
      "ciento ")}
acentena[3]:= {|| "doscient" + iif(ngrupo < 3 .AND. Arg2 = "F", ;
      "as ", "os ")}
acentena[4]:= {|| "trescient" + iif(ngrupo < 3 .AND. Arg2 = "F", ;
      "as ", "os ")}
acentena[5]:= {|| "cuatrocient" + iif(ngrupo < 3 .AND. Arg2 = ;
      "F", "as ", "os ")}
acentena[6]:= {|| "quinient" + iif(ngrupo < 3 .AND. Arg2 = "F", ;
      "as ", "os ")}
acentena[7]:= {|| "seiscient" + iif(ngrupo < 3 .AND. Arg2 = "F", ;
      "as ", "os ")}
acentena[8]:= {|| "setecient" + iif(ngrupo < 3 .AND. Arg2 = "F", ;
      "as ", "os ")}
acentena[9]:= {|| "ochocient" + iif(ngrupo < 3 .AND. Arg2 = "F", ;
      "as ", "os ")}
acentena[10]:= {|| "novecient" + iif(ngrupo < 3 .AND. Arg2 = "F", ;
      "as ", "os ")}
aconector[1]:= {|| ""}
aconector[2]:= {|| iif(agrupos[2] > "000", "mil ", "")}
aconector[3]:= {|| iif(agrupos[3] > "000" .OR. agrupos[4] > ;
      "000", iif(agrupos[3] = "001", "millon ", "millones "), "")}
aconector[4]:= {|| iif(agrupos[4] > "000", "mil ", "")}
aconector[5]:= {|| iif(agrupos[5] > "000", iif(agrupos[5] = ;
      "001", "billon ", "billones "), "")}
cnumstr:= alltrim(Str(Arg1, 15, 0))
cnumstr:= Replicate("0", 15 - Len(cnumstr)) + cnumstr
For ngrupo:= 1 to 5
   agrupos[5 - ngrupo + 1]:= SubStr(cnumstr, (ngrupo - 1) * 3 + 1, 3)
Next
cenletra:= ""
For ngrupo:= 5 to 1 step -1
   cunidad:= right(agrupos[ngrupo], 1)
   cdecena:= SubStr(agrupos[ngrupo], 2, 1)
   ccentena:= Left(agrupos[ngrupo], 1)
   cenletra:= cenletra + eval(acentena[Val(ccentena) + 1]) + ;
              eval(adecena[Val(cdecena) + 1][1]) + ;
              eval(aunidad[Val(cunidad) + 1]) + eval(aconector[ngrupo])
Next
Return Upper(cenletra) + ccentavos
Posts: 1380
Joined: Fri Oct 14, 2005 01:28 PM
Re: Petici贸n: c贸digo fuente de funci贸n Num2Letras
Posted: Sun Dec 30, 2012 12:50 AM
Aqu铆 otro; autor: Hern谩n Checcarelli
Code (fw): Select all Collapse
/***************
聽 聽Prog 聽 聽 聽 聽: NumLetra.prg
聽 聽Autor 聽 聽 聽 : Hernan Diego Checarelli
聽 聽M贸dulo 聽 聽 聽:
聽 聽Inicio 聽 聽 聽: 14/01/2010 - Bajado del Foro FiveTech
聽 聽 聽 聽 聽 聽 聽 聽***************************************
*/

#include "fivewin.ch"

FUNCTION NumLetras( nImporte, nDec )

聽 聽local cTexto:= '' /// Equivale a '999999999999.99'
聽 聽local cPicture:= 聽Replicate('9',12)
聽 聽local lSonSoloCentavos:= .F., nCpn
聽 聽local cNumero,aU,aD,aV,aC,aE,aM
聽 聽local nImporteDec:= 0

聽 聽DEFAULT nDec:= 2

聽 聽if nImporte < 0
聽 聽 聽 cTexto:= 'Menos'
聽 聽 聽 nImporte *=-1 聽 /// Lo paso a positivo
聽 聽endif

聽 聽if nDec == 0
聽 聽 聽 nImporte:= Int( nImporte )
聽 聽endif

聽 聽if nDec > 0
聽 聽 聽 cPicture+= '.' + Replicate('9', nDec)
聽 聽endif

聽 聽if nImporte < 1 .and. nImporte > 0 聽// Solo Decimales
聽 聽 聽 nImporteDec:= Val( SubStr( Transform( nImporte, "9.9999999999999999" ), 3, nDec ) )
聽 聽 聽 nImporte 聽 := Val( AllTrim(Str(Int(nImporte))) + "." + AllTrim(Str(Int(nImporteDec))) )
聽 聽endif

聽 聽//// Evaluo la cadena numerica ,dividiendola en cuadro partes:
聽 聽//// 999 - 999 - 999 - 999 y los decimales correspondientes.

聽 聽cNumero := Transform( nImporte , cPicture )
聽 聽nImporte:= Val( cNumero )

聽 聽do case
聽 聽 聽 case nImporte 聽== 0 .and. nImporteDec == 0
聽 聽 聽 聽 聽cTexto 聽:= 'Cero'

聽 聽 聽 case nDec > 0 .and. nImporteDec > 0
聽 聽 聽 聽 聽nImporte:= nImporteDec
聽 聽 聽 聽 聽cNumero := Transform( nImporte , cPicture )
聽 聽 聽 聽 聽lSonSoloCentavos:= .T.
聽 聽endcase


聽 聽aU:= {'Uno','Dos','Tres','Cuatro','Cinco','Seis','Siete','Ocho','Nueve'}
聽 聽aD:= {'Once','Doce','Trece','Catorce','Quince','Dieciseis','Diecisiete',;
聽 聽 聽 聽 聽'Dieciocho','Diecinueve'}
聽 聽aV:= {'Diez','Veinte','Treinta','Cuarenta','Cincuenta','Sesenta','Setenta',;
聽 聽 聽 聽 聽'Ochenta','Noventa'}
聽 聽aC:= {'Ciento','Doscientos','Trescientos','Cuatrocientos','Quinientos',;
聽 聽 聽 聽 聽'Seiscientos','Setecientos','Ochocientos','Novecientos'}
聽 聽aE:= {'Veintiuno','Veintidos','Veintitres','Veinticuatro','Veinticinco',;
聽 聽 聽 聽 聽'Veintiseis','Veintisiete','Veintiocho','Veintinueve'}
聽 聽aM:= {'Billones','Millones','Mil','/100'}


聽 聽for nCpn = 1 to 4 聽/// Los 4 '999'

聽 聽 聽 //// Analizo 1er. 9 ///
聽 聽 聽 if Val(SubStr(cNumero,1,1)) <> 0 聽// Si existe Centena !!!
聽 聽 聽 聽 聽do case
聽 聽 聽 聽 聽 聽 case SubStr(cNumero,1,3) == '100'
聽 聽 聽 聽 聽 聽 聽 聽cTexto:= AllTrim(cTexto) + Space(1) + SubStr(aC[1],1,4)
聽 聽 聽 聽 聽 聽 otherwise
聽 聽 聽 聽 聽 聽 聽 聽cTexto:= AllTrim(cTexto) + Space(1) + aC[Val(SubStr(cNumero,1,1))]
聽 聽 聽 聽 聽endcase
聽 聽 聽 endif

聽 聽 聽 //// Analizo 2do. 9 ///
聽 聽 聽 if Val(SubStr(cNumero,2,1)) <> 0 聽// Si existe Decena !!!
聽 聽 聽 聽 聽do case
聽 聽 聽 聽 聽 聽 case SubStr(cNumero,2,1) == '1' .and. SubStr(cNumero,3,1) <> '0'
聽 聽 聽 聽 聽 聽 聽 聽cTexto:= AllTrim(cTexto) + Space(1) + aD[ Val(SubStr(cNumero,3,1)) ]
聽 聽 聽 聽 聽 聽 case SubStr(cNumero,2,1) == '2' .and. SubStr(cNumero,3,1) <> '0'
聽 聽 聽 聽 聽 聽 聽 聽cTexto:= AllTrim(cTexto) + Space(1) + aE[ Val(SubStr(cNumero,3,1)) ]
聽 聽 聽 聽 聽 聽 otherwise
聽 聽 聽 聽 聽 聽 聽 聽cTexto:= AllTrim(cTexto) + Space(1) + aV[ Val(SubStr(cNumero,2,1)) ]
聽 聽 聽 聽 聽endcase
聽 聽 聽 endif

聽 聽 聽 //// Analizo 3er. 9 ///
聽 聽 聽 if Val(SubStr(cNumero,3,1)) <> 0 // Si existe la Unidad !!!
聽 聽 聽 聽 聽do case
聽 聽 聽 聽 聽 聽 case SubStr(cNumero,2,1) == '1' .or. SubStr(cNumero,2,1) == '2'
聽 聽 聽 聽 聽 聽 // Del 1 al 29 ya se habrian definido antes.-
聽 聽 聽 聽 聽 聽 otherwise
聽 聽 聽 聽 聽 聽 聽 聽if Val(SubStr(cNumero,1,2)) <> 0 .and. Val(SubStr(cNumero,2,1)) <> 0
聽 聽 聽 聽 聽 聽 聽 聽 聽 cTexto:= AllTrim(cTexto)+' y'
聽 聽 聽 聽 聽 聽 聽 聽endif
聽 聽 聽 聽 聽 聽 聽 聽cTexto:= AllTrim(cTexto) + Space(1) + aU[ Val(SubStr(cNumero,3,1)) ]
聽 聽 聽 聽 聽endcase

聽 聽 聽 聽 聽if nCpn <> 4 .and. SubStr(cNumero,3,1) == '1' .and. ;
聽 聽 聽 聽 聽 聽 SubStr(cNumero,2,1) <> '1'

聽 聽 聽 聽 聽 聽 cTexto:= SubStr( AllTrim(cTexto), 1, Len(AllTrim(cTexto))-1 )
聽 聽 聽 聽 聽 聽 /// Sino quedaria Uno millon o Uno mil => Le saco el ultimo caracter.

聽 聽 聽 聽 聽endif
聽 聽 聽 endif

聽 聽 聽 if nCpn < 4

聽 聽 聽 聽 聽 if ! Val(SubStr(cNumero,1,3)) == 0
聽 聽 聽 聽 聽 聽 聽cTexto:= AllTrim(cTexto) + Space(1) + aM[nCpn]
聽 聽 聽 聽 聽 endif

聽 聽 聽 聽 聽 if (nCpn == 1 .or. nCpn == 2) .and. Val(SubStr(cNumero,1,3)) == 1 聽/// ' 聽1'
聽 聽 聽 聽 聽 聽 聽cTexto:= SubStr( AllTrim(cTexto) , 1, Len(AllTrim(cTexto))-4) + '贸n'
聽 聽 聽 聽 聽 聽 聽/// Le saco el 'ones' y le pongo '贸n'
聽 聽 聽 聽 聽 endif

聽 聽 聽 endif

聽 聽 聽 cNumero:= SubStr( cNumero , 4 )

聽 聽next

聽 聽cNumero:= SubStr(cNumero,2,2) 聽/// Decimales del Nro.

聽 聽if Val(cNumero) <> 0 聽// Si tiene decimales
聽 聽 聽 cTexto:= AllTrim(cTexto)+' Con '+ cNumero +"/1"+Replicate("0",nDec)
聽 聽endif

聽 聽if lSonSoloCentavos

聽 聽 聽 cTexto:= StrTran(cTexto,'Uno','Un ')
聽 聽 聽 cTexto:= AllTrim(cTexto) + ' Centavo'
聽 聽 聽 if nImporte > 1
聽 聽 聽 聽 聽cTexto+= 's'
聽 聽 聽 endif

聽 聽 聽 if nDec <> 2 // No le Cabe la palabra "centavo<s>" cuando nDec no es 2.
聽 聽 聽 聽 聽cTexto:= "Cero Con " + AllTrim(Str(nImporteDec))+"/1"+Replicate("0",nDec)
聽 聽 聽 endif

聽 聽endif

RETURN( AllTrim( cTexto ) )
Resistencia - "Ciudad de las Esculturas"

Chaco - Argentina
Posts: 66
Joined: Thu Jan 03, 2013 06:13 PM
Re: Petici贸n: c贸digo fuente de funci贸n Num2Letras
Posted: Wed Jan 16, 2013 01:04 PM
Es Corta pero eficiente, adem谩s entrega el resultado en una sola l铆nea.

Code (fw): Select all Collapse
*------------------------------------------------------------------------------*
*Funci贸n de monto escrito
*para hacer funcionar, s贸lo haz el "Say PM_MS(Variable con el monto a transformar en letras)"
*As铆 de simple, si es en clipper DOS, es un Por Ejemplo @ 1,1 Say PM_MS(Monto)
********************************************************************************
Function PM_MS(Nro)
   local Unidad   :={'UN ','DOS ','TRES ','CUATRO ','CINCO ','SEIS ','SIETE ',;
                     'OCHO ','NUEVE '},;
         Decena   :={'DIE','VEINT','TREINTA ','CUARENTA ','CINCUENTA ',;
                     'SESENTA ','SETENTA ','OCHENTA ','NOVENTA '},;
         Centena  :={'CIEN','DOSCIENTOS ','TRESCIENTOS ','CUATROCIENTOS ',;
                     'QUINIENTOS ','SEISCIENTOS ','SETECIENTOS ',;
                     'OCHOCIENTOS ','NOVECIENTOS '},;
         UniEsp   :={'ONCE ','DOCE ','TRECE ','CATORCE ','QUINCE '},;
         Concepto :={'MILLON','MIL ','PESOS.-'},;
         NumConcep:={999999,999,0},;
         Digito[9],;
         C_Concep:=0, C_UDC:=0, C_Num:=0, C_Dig:=0, Punt:=0, Largo:=0, Aux:=0,;
         NumText:='', LargoNum:=0, NumCad:=transform(Nro,'999999999'), L:=0,;
         I:=0, J:=0

   L=len(NumCad)
   for I=1 to L
       if substr(NumCad,I,1)>='0' .AND. substr(NumCad,I,1)<='9'
   LargoNum++
       endif
   next
   for I=1 to 9
       Digito[I]=0
   next

   j=9
   for I=L to (L+1)-LargoNum step -1
       Digito[j]=val(substr(NumCad,I,1))
       j=j-1
   next
   for C_Concep=1 to 3
       for C_UDC=1 to 3
           C_Dig++
           Punt=Digito[C_Dig]
           if Punt!=0
              *** detnum ***
              Largo=0
              do case
                 case C_UDC=1
                      NumText+=Centena[Punt]
                 case C_UDC=2
                      Aux=Digito[C_Dig+1]
                      * NumText=''
                      if Punt>1 .OR. (Punt=1 .AND. (Aux = 0 .OR. Aux>5))
                         NumText+=Decena[Punt]
                      endif
                 case C_UDC=3
                      Aux=Digito[C_Dig-1]
                      if Aux=1 .AND. Punt<6
                         NumText+=UniEsp[Punt]
                      else
                         Aux=Digito[C_Dig-1]+Digito[C_Dig-2]
                         if .NOT. (Punt=1 .AND. C_Concep=2 .AND. Aux=0)
                            NumText+=Unidad[Punt]
                         endif
                      endif
                 endcase
              *** fin detnum ***
              if C_UDC=2
                 *** eval_espec ***
                 // NumText=''
                 do case
                    case Punt>2 .AND. Aux>0
                         NumText+='Y '
                    case Punt=1
                         if Aux=0
                            NumText+='Z '
                         else
                            if Aux>5
                               NumText+='CI'
                            endif
                         endif
                    case Punt=2
                         if Aux=0
                            NumText+='E '
                         else
                            NumText+='I'
                         endif
                 endcase
                 *** fin eval_espec ***
              else
                 if C_UDC=1 .AND. Punt=1
                    Aux=Digito[C_Dig+1]+Digito[C_Dig+2]
                    if Aux>0
                       NumText+='TO '
                    else
                       NumText+=' '
                    endif
                 endif
              endif
           endif
       next
       Aux=Digito[4]+Digito[5]+Digito[6]
       if Nro>NumConcep[C_Concep]
          if C_Concep!=2 .OR. (C_Concep=2 .AND. Aux>0)
             NumText+=Concepto[C_Concep]
          endif
       endif
       Aux=(Digito[1]*100)+(Digito[2]*10)+Digito[3]
       if C_Concep=1
          if Aux>1
             NumText+='ES '
          else
             NumText+=' '
          endif
       endif
   next

return NumText
Fivewin 12.04 + xHarbour 1.2.1 + BCC582



Nada como Fivewin, odio visual basic, visual studio y todas las porquer铆as visual.

Continue the discussion